My plot wont show up in the plot window.
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2015-11-7
Add
hold on
after the semilogy() call
2 个评论
Walter Roberson
2015-11-8
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!