My plot wont show up in the plot window.

1 view (last 30 days)
First off I left out some of the code because I want to focus on only Newtons method part of this. With inputs 1,2,0.75,0.001,30 I get the right values out but when the plot window comes out it doesn't show anything. I've tried setting up a zeroes array but the values wont go into the array. Any tips on how to fix this?
function [ ] = Project1(nFunction, nMethod, x0, epsilon, nStop)
f1 = @(x) x.^3-3*x.^2+3*x-1;
df1 = @(x) 3*x.^2-6*x+3;
f2 = @(x) tan( x );
df2 = @(x) sec( x ).^2;
f3 = @(x) x+cos(x) .* exp(-50*x.^2);
df3 = @(x) 1-exp(-50*x.^2).*(100*x*cos(x)+sin(x));
n=0;
if ( nFunction == 1 )
f=f1;
xs=1;
elseif ( nFunction == 2 )
f=f2;
xs=pi;
else
f=f3;
xs=-0.1832913;
end;
if ( nMethod == 1 )
bisection( f );
elseif ( nMethod == 2 )
if ( nFunction == 1 )
df=df1;
elseif ( nFunction == 2 )
df=df2;
else
df=df3;
end
newton( f,df );
else
secant( f );
end
function [] = newton( f ,df )
while(abs(xs-x0)> epsilon && n <= nStop);
n=n+1;
x = x0-f(x0)/df(x0);
x0=x;
disp( sprintf( 'Approximate using Newtons = %e', x ));
disp( sprintf( 'Error = %e', xs-x0 ));
semilogy( n, f(x), 'b-');
end
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2015
Add
hold on
after the semilogy() call
  2 Comments
Sean Poitou
Sean Poitou on 7 Nov 2015
It changed the axis numbers to something that looks more reasonable but still no line. :\
Walter Roberson
Walter Roberson on 8 Nov 2015
Of you want a line you need to plot more than one point at a time. Matlab does not join points in different plotting calls. Make a vector of results and plot the vector

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!