Plot in Bisection Method
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Utku Palakci
el 2 de En. de 2019
Respondida: Geoff Hayes
el 2 de En. de 2019
%% Bisection Method
% f(x) = 2sin(x) + 2cos(x)
a = -1;
b = 1;
tol = 10^-3;
iter = 0;
fprintf('iter \t a \t b \t fark\n' );
fprintf('---------------------------------------------\n' );
while (abs(a-b)>tol)
fa = 2*sin(a) + 2*cos(a);
fb = 2*sin(b) + 2*cos(b);
m = (a+b)/2;
fm = 2*sin(m) + 2*cos(m);
if (fa*fm<0)
b=m;
else
a=m;
end
iter = iter + 1 ;
fprintf('%d \t %f \f \t %f \t %f \t %f\n',iter,a,b, abs(a-b),tol);
end
Hey everyone
How can I plot this code with functions showing the location of the
initial guess and the approximated root value?
0 comentarios
Respuesta aceptada
Geoff Hayes
el 2 de En. de 2019
Utku - I suppose that you would want to plot the m that is generated on each iteration of the loop. If that is the case, you could save that data to an array and plot that array when you exit the loop like
iter = 1;
mData = []; % create an array
while (abs(a-b)>tol)
fa = 2*sin(a) + 2*cos(a);
fb = 2*sin(b) + 2*cos(b);
m = (a+b)/2;
fm = 2*sin(m) + 2*cos(m);
if (fa*fm<0)
b=m;
else
a=m;
end
mData[iter] = m; % save data to array
iter = iter + 1 ;
fprintf('%d \t %f \f \t %f \t %f \t %f\n',iter,a,b, abs(a-b),tol);
end
plot(mData); % plot data
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!