Write a MATLAB script that will find the roots of a given equations using the BI
ID: 3028124 • Letter: W
Question
Write a MATLAB script that will find the roots of a given equations using the BISECTION METHOD. Format your output to look similar to the examples given. You should write your output to a file. Set the maximum number of iterations to 100 and the tolerance to 0.000005. Use a tolerance definition of |b_n - a_n/2| where a_n & b_n are the interval endpoints for the nth iteration Use the script to solve the given equations. Use the interval endpoints as your initial a and b. Eql f(x) = e^x - (x^2 + 4) = 0 on [2, 3] Eq2 f(x) = x - 2^x = 0 on [1/3, 1] Eq3 f(x) = x^3 - 1.8999 x^2 + 1.5796 x- 2.1195 = 0 on [1, 2].Explanation / Answer
For Eq1 Matlab code
clc;
clear all;
s=1200;
h=20;
f=@(x) exp(x)-(x^2+4) ;
a=2;
b=3;
format long
eps_abs = 0.000005;
eps_step = 0.000005;
n=0;
while ((b - a )/2>= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs )&& n~=100 )
n=n+1;
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
err=abs( (b-a)/2);
end
n
c
err
output
n =
18
c =
2.158725738525391
err =
1.907348632812500e-006
For EQ2
change the function f(x) and interval a=1/3 , b=1 in above code (remaining same code)
clc;
clear all;
s=1200;
h=20;
f=@(x) x-2^(-x) ;
a=1/3;
b=1;
format long
eps_abs = 0.000005;
eps_step = 0.000005;
n=0;
while ((b - a)/2 >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs )&& n~=100 )
n=n+1;
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
err=abs( (b-a)/2);
end
n
c
err
out put
n =
17
c =
0.641189575195313
err =
2.543131510435170e-006
For EQ3
change the function f(x) and interval a=1 , b=2 in above code (remaining same code)
Output
n =
17
c =
1.367546081542969
err =
3.814697265625000e-006
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.