Can't get plot to work

조회 수: 2 (최근 30일)
Matt Baron
Matt Baron 2021년 3월 25일
댓글: Matt Baron 2021년 3월 27일
Hello Mathworks team!
I created this Euler's method code below but can't get the graph to show anything at all. Even if I make a separate code with just xn=1 yn=1, plot(xn,yn), the graph shows up but nothing on it. Any help would be much appreciated.
%Euler's Method
h=.25;%step size
xinitialcondition=0;%initial condition for x
yinitialcondition=1;%initial condition for y
n=0;%start at 0
xn=xinitialcondition;
yn=yinitialcondition;
f=@(xn,yn) (2*(cos(xn))*yn);%the function to be evaluated
while xn < 10%what value do you want it estimated to
xn = (xinitialcondition) + ((n) .* (h));
[n xn yn]
yn = (yn) + ((h) .* f(xn,yn));
if xn >= 10
break
end
plot(xn,yn,"r-")
hold on
n=n+1;
end
hold off

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 25일
%Euler's Method
h=.25;%step size
xinitialcondition=0;%initial condition for x
yinitialcondition=1;%initial condition for y
n=0;%start at 0
xn=xinitialcondition;
yn=yinitialcondition;
f=@(xn,yn) (2*(cos(xn))*yn);%the function to be evaluated
while xn < 10%what value do you want it estimated to
xn = (xinitialcondition) + ((n) .* (h));
[n xn yn]
yn = (yn) + ((h) .* f(xn,yn));
if xn >= 10
break
end
plot(xn,yn,"r-*") %CHANGED
hold on
n=n+1;
end
ans = 1×3
0 0 1
ans = 1×3
1.0000 0.2500 1.5000
ans = 1×3
2.0000 0.5000 2.2267
ans = 1×3
3.0000 0.7500 3.2037
ans = 1×3
4.0000 1.0000 4.3758
ans = 1×3
5.0000 1.2500 5.5579
ans = 1×3
6.0000 1.5000 6.4342
ans = 1×3
7.0000 1.7500 6.6618
ans = 1×3
8.0000 2.0000 6.0681
ans = 1×3
9.0000 2.2500 4.8055
ans = 1×3
10.0000 2.5000 3.2961
ans = 1×3
11.0000 2.7500 1.9758
ans = 1×3
12.0000 3.0000 1.0627
ans = 1×3
13.0000 3.2500 0.5367
ans = 1×3
14.0000 3.5000 0.2699
ans = 1×3
15.0000 3.7500 0.1435
ans = 1×3
16.0000 4.0000 0.0846
ans = 1×3
17.0000 4.2500 0.0570
ans = 1×3
18.0000 4.5000 0.0443
ans = 1×3
19.0000 4.7500 0.0396
ans = 1×3
20.0000 5.0000 0.0403
ans = 1×3
21.0000 5.2500 0.0461
ans = 1×3
22.0000 5.5000 0.0579
ans = 1×3
23.0000 5.7500 0.0784
ans = 1×3
24.0000 6.0000 0.1121
ans = 1×3
25.0000 6.2500 0.1659
ans = 1×3
26.0000 6.5000 0.2489
ans = 1×3
27.0000 6.7500 0.3704
ans = 1×3
28.0000 7.0000 0.5358
ans = 1×3
29.0000 7.2500 0.7377
ans = 1×3
30.0000 7.5000 0.9472
ans = 1×3
31.0000 7.7500 1.1114
ans = 1×3
32.0000 8.0000 1.1691
ans = 1×3
33.0000 8.2500 1.0840
ans = 1×3
34.0000 8.5000 0.8749
ans = 1×3
35.0000 8.7500 0.6116
ans = 1×3
36.0000 9.0000 0.3728
ans = 1×3
37.0000 9.2500 0.2030
ans = 1×3
38.0000 9.5000 0.1030
ans = 1×3
39.0000 9.7500 0.0517
ans = 1×3
40.0000 10.0000 0.0272
hold off
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 3월 25일
plot() only draws a line if there are two adjacent finite values in the same call. You are only plotting one point at a time, so it cannot draw lines.
Matt Baron
Matt Baron 2021년 3월 25일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2021년 3월 25일
Save into a Matrix and plot all at once:
%Euler's Method
h=.25;%step size
xinitialcondition=0;%initial condition for x
yinitialcondition=1;%initial condition for y
n=0;%start at 0
xn=xinitialcondition;
yn=yinitialcondition;
f=@(xn,yn) (2*(cos(xn))*yn);%the function to be evaluated
A = zeros([],2) ;
while xn < 10%what value do you want it estimated to
xn = (xinitialcondition) + ((n) .* (h));
yn = (yn) + ((h) .* f(xn,yn));
if xn >= 10
break
end
n=n+1;
A(n,:) = [xn yn] ;
end
plot(A(:,1),A(:,2))
  댓글 수: 6
Matt Baron
Matt Baron 2021년 3월 27일
Do I have the questions correct in bold below;
A = zeros([],2) ; %so this is like creating an empty matrix that will be x by 2 for future use?
while xn < 10%what value do you want it estimated to
xn = (xinitialcondition) + ((n) .* (h));
yn = (yn) + ((h) .* f(xn,yn));
if xn >= 10
break
end
n=n+1;
A(n,:) = [xn yn] ; %this is inputting xn yn into the nth row of matrix A?
end
plot(A(:,1),A(:,2)) %this plots matrix A all rows of column 2 vs matrix A all rows of column 1?
Matt Baron
Matt Baron 2021년 3월 27일
It wasn't working with n=1 so I re-wrote the code using matrices according to your code above and it worked!!
This looks so much better.
h=.05;%step size
init=[1 1];%initial conditions for x and y
n=1;%start at 1
x=init(1,1);
y=init(1,2);
A=zeros([],2);%create an empty 0 by 2 matrix for later
f=@(x,y) (.2 * x * y);%the function to be evaluated
while x <= 1.5%what value do you want it estimated to
A(n,:)=[x y];%input x and y into row n
y = (y + ((h) * f(x,y)));
x = (init(1,1) + ((n) * (h)));
if x > 1.5
break
end
n=n+1;
end
plot (A(:,1),A(:,2))%plot y vs x

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by