Why is the loop infinite?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Trying to do False Position method for the taylor series expansion of sin(x)= sigma(sum)]k=0=inf ((-1^k)/(2*p+1)!)*((x)^(2*k+1)). Input k, bounds between a small number>0 and 3pi/2, Error<.0001. Function should also find the error between the root of the series and the actual root of the function. Error = abs(actual_value-estimate/ actual_value)Actual root should equal pi/2 because I'm solving for sin(x)=0 right? There are two outputs for the function, the estimate root and the error of the root. On one figure plot: 1) Sin(x)-- 2) The polynomial produced from the first k terms of the Taylor expansion-- 3) The false position guesses-- 4). The actual root of sin(x) Code:
function [root,root_error]= HW7_JAS_2(k)
x_lower=.000001;%initial lower bound
x_upper=(3*pi)/2;%initial upper bound
x_true=pi/2;%actual value for x
x_root=0;
lower_b=zeros(1,k+1);
upper_b=zeros(1,k+1);
it=1;
while abs((x_true-x_root)/x_true) > .0001
for p=0:k
lower_b(p+1)=((-1^p)/factorial(2*p+1))*((x_lower).^(2*p+1));%put through taylor series expansion
upper_b(p+1)=((-1^p)/factorial(2*p+1))*((x_upper).^(2*p+1));%put through taylor series expansion
end
sumlower_b=sum(lower_b);%f(lower bound)
sumupper_b=sum(upper_b);%f(upper bound)
x_root=(x_upper)-(((sumlower_b)*(x_lower-x_upper))/(sumlower_b-sumupper_b));%calculates root
fx_root=sin(x_root);%f(root)
if lower_b * fx_root <0
x_upper= x_root;
elseif upper_b * fx_root <0
x_lower= x_root;
elseif lower_b * fx_root ==0
root = x_root;
end
plot(it,x_root,'g*');%plots false position guesses
hold on%allows each guess to be plotted
xlabel('Iterations');
ylabel('Estimated time values');
legend('Iterations vs Estimated time values');
it=it+1;%new iteration
end
root_error= abs((x_true-x_root)/x_true);%error in the root
x=0:.1:2*pi;
plot(x,sin(x),'r-');
xlabel('x values');
ylabel('sin(x) values');
legend('sin(x)');
hold on
x1=0:.1:root;
plot(x1,sin(root),'b-');
xlabel('x values stopped at root value');
ylabel('sin(x) values stopped at sin(root)');
legend('sin(root)');% The polynomial produced from the first k terms of the Taylor expansion
hold on
plot(x_true,sin(x_true),'yx');
xlabel('x values');
ylabel('y values');
legend('Real root of sin(x)');
end
Posted this earlier but the website deleted it for some reason. Thanks for any help. Posted
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!