d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Loop Question
Add Reply New Topic New Poll
Member
Posts: 17,018
Joined: Jul 8 2007
Gold: 0.00
Sep 24 2012 04:06pm
say you have a function

y=10+2k

so when
k=0, y=10
k=1, y=12
k=2, y=14

im trying to make a loop so that the current function is added to the previous function

so when k is equal to 2 it should output 26

it would be best to code in python please

to be more specific... im trying to make it so that the function S keeps adding to its previous function in the loop untill Sp1-S>0.01

Code
function S=matcos(A)

k=0;
S= (-1)^(k)*A^(2*k)/factorial(2*k)
Sp1= (-1)^(k+1)*A^(2*(k+1))/factorial(2*(k+1))

while somefunc max(abs(Sp1-S))>0.01;



   k=k+1;
   Sp1=S+%something
end
end


This post was edited by xMaxPower on Sep 24 2012 04:16pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2012 04:16pm
sum = 0

for i in range (0, 2):
sum += 10 + 2 * i

something to that effect
Member
Posts: 17,018
Joined: Jul 8 2007
Gold: 0.00
Sep 24 2012 07:09pm
Code
function S=assign1testing(A)

k=1;
Sp1=0;
S= (-1)^(k)*A^(2*k)/factorial(2*k);

while abs(max(Sp1-S))>0.01;

   Sm1= (-1)^(k-1)*A^(2*(k-1))/factorial(2*(k-1));
   S=S+Sm1;
   k=k+1;
   Sp1=S+(-1)^(k+1)*A^(2*(k+1))/factorial(2*(k+1));

end

end
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2012 07:16pm
you said that's python? the function header looks more like matlab than python imo. you have some strange syntax which i'm not sure is correct, but i dont know what language that's in. for example, you have a semi colon at the end of the while loop. and you use ^ which i'm not 100% what it does in python. i assume it does bitwise xor since ** is exponentiation.

This post was edited by carteblanche on Sep 24 2012 07:21pm
Member
Posts: 17,018
Joined: Jul 8 2007
Gold: 0.00
Sep 24 2012 09:07pm
Quote (carteblanche @ Sep 25 2012 01:16am)
you said that's python? the function header looks more like matlab than python imo. you have some strange syntax which i'm not sure is correct, but i dont know what language that's in. for example, you have a semi colon at the end of the while loop. and you use ^ which i'm not 100% what it does in python. i assume it does bitwise xor since ** is exponentiation.


it was actually matlab. i dont no why but i thot no1 would no what it was so i was just gonna translate python to matlab. i dont need help anymore tho thx
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll