Question

Using MATLAB or FreeMat

----------------------------

Bisection Method and Accuracy of Rootfinding Consider the function f(0) = 3 cos 2r cos 4-2 cos Garcos 3r - 6 cos 2r sin 2r-5.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


%&Matlab code for Bisection method clear all close all function for which root have to find fun @(x) 3.*cos(2.*x).*cos(4.*)-2 xx=-5:1:5; for i=1:length(xx) rr=root+xx(i)*10^-10; fprintf(\t%f+({d*10^-10 )= te\n,root, xx(i), fun(rr)) end Matlab functi0.567042+(-2*10^-10)= -2.875361e-09 0.567042+(-1*10^-10)= -1.458817e-09 0.567042+(0*10^-10)= -4.22724 le-11 0.567042+(1+10^-1X VS. f(x) plot fo 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 9 0. 1 X VS. f(x) plot 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 9 0. 1

%%Matlab code for Bisection method
clear all
close all

%function for which root have to find
fun=@(x) 3.*cos(2.*x).*cos(4.*x)-2.*cos(6.*x).*cos(3.*x)-6.*cos(2.*x).*(sin(2.*x)).^2-5.*cos(3.*x)+5/2;
xx=linspace(0,1);
yy=fun(xx);
fprintf('for the function f(x)=')
disp(fun)
figure(1)
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('x vs. f(x) plot')

[root]=bisection_method(fun,0,1,1000);
fprintf('\tRoot using Bisection method is %f.\n',root)
hold on
plot(root,fun(root),'r*')
grid on; box on;

%%table for x and f1(x)
fprintf('\n\t x\t\t\tf(x)\n')
xx=-5:1:5;
for i=1:length(xx)
    rr=root+xx(i)*10^-10;
    fprintf('\t%f+(%d*10^-10)= %e\n',root,xx(i),fun(rr))
end

%function for which root have to find
fun=@(x) 3.*cos(2.*x).*cos(4.*x)-2.*cos(6.*x).*cos(3.*x)-6.*cos(2.*x).*(sin(2.*x)).^2-5.*cos(3.*x)+7/2;
xx=linspace(0,1);
yy=fun(xx);
fprintf('for the function f(x)=')
disp(fun)
figure(2)
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('x vs. f(x) plot')

[root]=bisection_method(fun,0,1,1000);
fprintf('\tRoot using Bisection method is %f.\n',root)
hold on
plot(root,fun(root),'r*')
grid on; box on;

%%table for x and f1(x)
fprintf('\n\t x\t\t\tf(x)\n')
xx=-5:1:5;
for i=1:length(xx)
    rr=root+xx(i)*10^-10;
    fprintf('\t%f+(%d*10^-10)= %e\n',root,xx(i),fun(rr))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Matlab function for Bisection Method
function [root]=bisection_method(fun,x0,x1,maxit)
if fun(x0)<=0
    t=x0;
    x0=x1;
    x1=t;
end
fprintf('\nRoot using Bisection method\n')
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>10^-10
    count=count+1;
    xx(count)=(x0+x1)/2;
    mm=double(fun(xx(count)));
    if mm>=0
        x0=xx(count);
    else
        x1=xx(count);
    end
    err(count)=abs(fun(x1));
    k=abs(fun(x1));
    if count>=maxit
        break
      
    end
    %
end
fprintf('\tAfter %d iteration root using Bisection method is %f\n',count,xx(count))
root=xx(end);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
Using MATLAB or FreeMat ---------------------------- Bisection Method and Accuracy of Rootfinding Consider the function f(0) =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT