how to use a while loop to perform an infinite series approximation
Show older comments
So I have to write a program using a while loop that uses the alternating series approximation for sin(x) using a while loop and compare it to the matlab sin(x) using an error function. so far I have:
clc
clear
x=input('Enter the value of x to compute sin(x): ');
error=1;
n=0;
while error >= 1*(10^-5);
terms = ((-1)^n)*(((x^(n+1)))/factorial(n+1));
SINx=sum(terms);
n=n+1;
error=abs((sin(x)-SINx)/sin(x))*100;
end
And my loop does nothing. Any ideas?
1 Comment
steven mcquade
on 1 Nov 2016
you haven't told it to print an answer
Accepted Answer
More Answers (1)
Walter Roberson
on 2 Jul 2012
0 votes
You are only generating a single value in "terms", and the sum() is being applied to that single value. You are not accumulating the values from the terms with n=0, n=1, n=2, etc..
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!