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.

101 views (last 30 days)
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 Comment
Star Strider
Star Strider on 5 Mar 2015
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.

Sign in to comment.

Answers (1)

Daksh
Daksh on 31 Jan 2023
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.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!