My plot wont show up in the plot window.

조회 수: 1 (최근 30일)
Sean Poitou
Sean Poitou 2015년 11월 7일
댓글: Walter Roberson 2015년 11월 8일
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

채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 7일
Add
hold on
after the semilogy() call
  댓글 수: 2
Sean Poitou
Sean Poitou 2015년 11월 7일
It changed the axis numbers to something that looks more reasonable but still no line. :\
Walter Roberson
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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by