stuck on taylor seires

edited February 2010 in Science & Tech
The problem: how do I make the sum = the calculated values added together
Should I use another "for loop" or maybe a "while loop"?
Or have I gone about the code completely wrong?
x=input('value of x:');
n=input('number of interations:');
i=0:n;
for k=1:n
tscos(1,k)=((-1)^(i))*((x^(i*2))/(factorial(2*i)));
end

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2010
    cfkou wrote:
    The problem: how do I make the sum = the calculated values added together
    Should I use another "for loop" or maybe a "while loop"?
    Or have I gone about the code completely wrong?
    x=input('value of x:');
    n=input('number of interations:');
    i=0:n;
    for k=1:n
    tscos(1,k)=((-1)^(i))*((x^(i*2))/(factorial(2*i)));
    end
    

    you have a couple options:

    x=input('value of x:');
    n=input('number of interations:');
    i=0:n;
    acc = 0;
    for k=1:n
    tscos(1,k)=((-1)^(i))*((x^(i*2))/(factorial(2*i)));
    acc = acc + tscos(1,k);
    end
    

    or

    x=input('value of x:');
    n=input('number of interations:');
    i=0:n;
    for k=1:n
    tscos(1,k)=((-1)^(i))*((x^(i*2))/(factorial(2*i)));
    end
    acc = sum(tscos);
    

    acc will contain the sum, in either case.
Sign In or Register to comment.