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.

84 次查看(过去 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