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.

77 visualizaciones (últimos 30 días)
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 comentario
Star Strider
Star Strider el 5 de Mzo. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Daksh
Daksh el 31 de En. de 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.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by