In: Computer Science
MATLAB Coding. Please answer with respected Matlab code format
HW 2_5
Determine the two roots of f(x) = 2x cos(2x) − (x − 2)^2 , using falsepos.p, accurate to 3 sig figs. Plot these points on the graph with a star marker.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
clc
clear all
close all
f = @(x) 2*x.*cos(2*x)-(x-2).^2;
hold on;
fplot(f,[0,4]);
a1=[2,3.2];
a2=[3,4];
for kk=1:2
e=1e-3;
x0=a1(kk);
x1=a2(kk);
i=0;
imax=200;
x2=x1+1;
flag=1;
while (abs(x2-x1)>e)
if(i~=0)
x1=x2;
end
x2=x1-((f(x1)/(f(x1)-f(x0)))*(x1-x0));
x0=x1;
i=i+1;
if(i==200)
disp('the required accuracy is not reached in 100
iterations');
flag=0;
end
end
if(flag==1)
root=x2;
else
root=[];
end
fprintf('x is %.3f\n',root);
plot(root,f(root),'*');
end
Kindly revert for any queries
Thanks.