Question in Picture. Need a matlab code. Thank you. Tavlor series, while loop an
ID: 3880410 • Letter: Q
Question
Question in Picture. Need a matlab code. Thank you.
Tavlor series, while loop and error calculation 6. Consider the function f(x) = x + 2 0.152 sin x. around the point is xo- Develop a Matlab script to determine the lowest order of Taylor expansion resulting in a maximum error of 0.00001% for all values on the interval specified below. Write the Maltab script (using a while loop) so that the approximate function value and true relative error (Et) is calculated for each order Taylor expansion, i.e from zeroth-order to the highest order needed, for the values [0. /6, 2/6, 3/6, 416, 5t/6, ]. The result should be two 2D arrays where each row is a specific order in the Taylor expansion, and one array holds the function value at each x point (the columns), and the other array hold the error value at each x point Matlab functions and scripts may be utilized, but the parent calling program should be a Matlab script (the parent script run from the command line is sometimes called the wrapper since it wraps around the other scripts and functions) Below is an example of the 2D arrays for 0h through 5th order calculations. Your maximum order could be different Taylor x values Order T11 T12 T13 T21 T22 14 15 16 st f(x) T(6 rows x 7 columns) 3r 4 th Order 12 16 17 1S st E21 E22 True Error in T(x) = (6x7) = nd 3Explanation / Answer
Code below is created in matlab 2017a. Diff function is used so that even if the given function is changed, there won't be much change in the code
Program:
syms x %defining x as variable
f(x)=x+2+0.512*sin(x); %defining the function
digitsOld = digits(50); %increasing the precision
values=[0,pi/6,2*pi/6,3*pi/6,4*pi/6,5*pi/6,pi]; %given values
i=0;
%vpa is used to display the max number of digits
for j=1:7
tval(j)=vpa(f(values(j))); %calculating true values
end
while true
count=0;
for j=1:7
out(i+1,j)=vpa(taylor_series(values(j),i)); %to calculate value
error(i+1,j)=tval(j)-out(i+1,j); %to calculate error
per=error(i+1,j)/tval(j); %to calculate error percent
if abs(per)<=0.00001
count=count+1;
end
end
if count==7
break; %loop gets break when all have maximum error 0.000001
end
i=i+1;
end
out
error
%function for taylor series
function res=taylor_series(y,i)
syms x;
f(x)=x+2+0.512*sin(x);
j=0;a=(3*pi)/7;
res=f(a);
while j<i
f=diff(f);
t=vpa(f(a))*vpa(((y-a).^j)/factorial(j));
res=res+t;
j=j+1;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.