evaluating cos(x)
need to evaluate it to whatever order term i want. I also need to display the relative error each time. My only problem is getting the sign to switch every other term. heres what I have:
[php]
function y=democos(x)
yexact=cos(x)
nmax=input('Enter highest order to which you want to evaluate:')
y=1
for i=1:nmax
y=y+(-1)(x^(i*2))/factorial(i*2)
error=((yexact-y)/yexact)*100
[/php]
[php]
function y=democos(x)
yexact=cos(x)
nmax=input('Enter highest order to which you want to evaluate:')
y=1
for i=1:nmax
y=y+(-1)(x^(i*2))/factorial(i*2)
error=((yexact-y)/yexact)*100
[/php]
0
Comments
updated code:
I know the problem is somewhere between the = sign and first * in that 7th line. Obviously its just subtracting each term over and over instead of alternating +/-.
Thanks for any help.
When i is even, -1^i = 1. When i is odd, -1^i = 1.
You may need to add or subtract 1 from i to make sure it is even in the correct indicies.
Thanks.. don't know why I didn't think of that. What do you mean in the correct indicies? There is something still wrong I can't figure it out.. my answers aren't converging towards the true value..
I am running it at cos(1.5) to 8 degrees.
It's giving me this:
It is closer to yexact at the beginning? What the fuzzy no nosed chimp?
it should be (-1)^i instead of -1^i
gives me the same answers?