How to set the domain to plot? I want my domain to be 0.18 to 0.3 so I can see the graph better. Currently it looks like a bar.

조회 수: 69 (최근 30일)
clear; clc;
% set initial conditions
p0 = 0.1;
p(1)=p0;
i=1; % set counter for matrix "plot"
% Apply Euler Method
for h=0.18:0.001:0.3
for n=1:230
p(n+1)=(1+10*h)*p(n)-(10*h)*(p(n))^2;
if n>200
P(i)=p(n);
i=i+1;
end
end
end
% Display results graphically
plot(h, P,'b+','LineWidth',2);
  댓글 수: 1
Star Strider
Star Strider 2015년 3월 5일
In your plot call, ‘h’ will be the last value assigned to it, here 0.3, so it is plotting as a bar since everything is plotting at 0.3. When I used the ‘h’ vector in your plot call, it turns out that ‘h’, ‘p’, and ‘P’ are all different lengths, so you can’t plot them against each other.
It’s not clear to me what you’re doing, so I can’t comment further on how to resolve this.

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

답변 (1개)

Daksh
Daksh 2023년 1월 31일
I understand that you wish to plot a graph with a given domain range on the x-axis, instead of an unexpected bar plot.
The code provided by you produces the following output with fixed x value and no domain value, as the variable 'h' provided in your plot() method call has the scalar value 0.3 and not a vector value (due to incorrect value assignment to variable).
clear; clc;
% set initial conditions
p0 = 0.1;
p(1)=p0;
i=1; % set counter for matrix "plot"
% Apply Euler Method
for h=0.18:0.01:0.3
for n=1:230
p(n+1)=(1+10*h)*p(n)-(10*h)*(p(n))^2;
if n>200
P(i)=p(n);
i=i+1;
end
end
end
% Display results graphically
plot(h, P,'b+','LineWidth',2);
% plot(0.18:0.05:0.3, P,'b+','LineWidth',2);
In the code provided above, if you uncomment the last line with correct vector value assigned to 'h', it will yield you an error as variables 'P' and 'h' have different lengths, hence they can't be plotted together using MATLAB. Hence you need to redesign your coding algorithmic logic to accommodate a new vector variable 'P' that has the same length as 'h' so that they can be plotted for the range that corresponds to variable 'h'.
Hope that helps.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by