hello everyone, kindly help me to how to join all points with line.. here is my code.
function bisection
func=@(n) 40*n^1.5-875*n+35000;
maxit=50;
es=0.0001;
xl=50;
xu=100;
test = func(xl)*func(xu);
if test>0,error('no sign change'),end
mp = linspace(xl,xu);
fp = 40*mp.^1.5-875*mp+35000;
iter = 0; xr = xl; ea = 100;
fprintf('at iteration %2i\nxl = %2i\nxu = %2i\n ', 0, xl,xu)
fprintf(' iteration xl xu xr f(x) ea')
while (1)
% plot(iter,ea,'-*');axis ([0 20 -5 16])
xrold = xr;
xr = (xl + xu)/2;
hold on
plot(iter,ea,'r.-','markersize',20); axis([0 20 -5 40])
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl)*func(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
fprintf('\n%10i %14.4f %10.4f %10.3f %10.3f %9.3f', iter,xl,xu,xr,func(xr),ea)
end
fprintf('\n\nRoot is %7.3f after %2i iterations. \n\n', xr, iter)
root = xr; fx = func(xr);
fprintf('\nerror is = %17f\ntrue root=%17.4f\niteretions taken= %10.4f\n F(n)=%20.4f\n',ea,xr,iter,func(xr));
end

 Réponse acceptée

Star Strider
Star Strider le 27 Nov 2017
First, remove the plot call from the loop.
Second, create vectors for ‘ea’ and ‘iter’.
Third, plot the vectors after the loop:
while (1)
% plot(iter,ea,'-*');axis ([0 20 -5 16])
xrold = xr;
xr = (xl + xu)/2;
% hold on
% plot(iter,ea,'r.-','markersize',20); axis([0 20 -5 40])
iter = iter + 1;
if xr ~= 0
ea = abs((xr - xrold)/xr) * 100;
end
test = func(xl)*func(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
eav(iter) = ea; % Vector: ‘ea’
iterv(iter) = iter; % Vector: ‘iter’
if ea <= es | iter >= maxit,break,end
fprintf('\n%10i %14.4f %10.4f %10.3f %10.3f %9.3f', iter,xl,xu,xr,func(xr),ea)
end
and:
figure(1)
plot(iterv,eav,'r.-','markersize',20);
axis([0 20 -5 40])

2 commentaires

Azher Jabbar
Azher Jabbar le 27 Nov 2017
Thanks For your valuable time.. it works perfect
Star Strider
Star Strider le 27 Nov 2017
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by