Bisection method arranging the output as a table
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Write a MATLAB code for the Bisection Method (Algorithm 2.1) and use it to find
approximation to the root of the following function:
f(x) = x^3 + 4x^2 - 10 on the interval [1; 2]
using TOL = 10^-4.
Arrange your output in a table similar to Table 2.1 in your textbook
My question is that How can I print an output contain a table like this???

y = @(x) x^3 + (4* x^2) -10;
x1= 1;
x2 = 2;
TOL = 10^-4;
N = 100;
eps=abs(x2-x1);
i=1;
FA = y(x1);
while i <= N
p = x1 + (x2 - x1 )/2 ;
FP = y(p);
if FP == 0 || (x2 - x1)/2 < TOL
p
break ;
end
i = i +1;
if FA * FP > 0
x1 = p;
FA = FP ;
else
x2 = p;
end
end
fprintf('Method failed after %d iterations, N= %d',N, N);
return;
0 Kommentare
Antworten (1)
David Hill
am 16 Sep. 2020
function t = bisection(f,int,tol)%f=@(x)x^3 + 4*x^2 - 10, int=[1,2],tol=1e-4
format long;
sz=[32,5];
varTypes={'double','double','double','double','double'};
varNames={'n','an','bn','pn','fpn'};
t=table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
t.n(1)=1;
t.an(1)=int(1);
t.bn(1)=int(2);
t.pn(1)=(t.an(1)+t.bn(1))/2;
t.fpn(1)=f(t.pn(1));
count=1;
while (t.bn(count)-t.an(count))>tol%notsure how you want to be measuring your error
count=count+1;
t.n(count)=count;
if t.fpn(count-1)>0
t.bn(count)=t.pn(count-1);
t.an(count)=t.an(count-1);
else
t.an(count)=t.pn(count-1);
t.bn(count)=t.bn(count-1);
end
t.pn(count)=(t.an(count)+t.bn(count))/2;
t.fpn(count)=f(t.pn(count));
end
2 Kommentare
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!