Trying to plot multiple Euler curves with 3 different step values.

5 views (last 30 days)
Ive gotten this far and have tried other methods but i think this is kind of the way i needed to go. Any help would be great! Thanks! I havent had much or really any Matlab training.
clear t
clear x
a = 0;
b = 30;
x0 = 6;
h = (b-a)/N;
t(1)= a;
x(1) = x0;
for N=60
x(n+1)=x1;
for N=300
x(n+1)=x2;
for N=3000
x(n+1)=x3;
t(n+1)=t(n)+ h;
x(n+1)= x(n)+ h.*0.65.*x(n).*(1-(x(n)/5.4));
end
hold on
plot(t,x3)
end
hold on
plot(t,x2)
end
hold on
plot(t,x1)

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Sep 2021
There are a few unknowns and flaws in your code, e.g.:
N, x1, x2, x3 are not known
Within loops: N=60, N = 300, N = 3000 are not correctly defined. If you need to compute x and t for n times, then the loops need to be n=1:60, n=1:300, n=1:3000
  2 Comments
Zachary David
Zachary David on 30 Sep 2021
I am trying to get different step sizes 0.5, 0.1, and 0.01 then plot them and N is the way to get those in the for loops and once it calculates it needs to put it in the plot. I think that is what youre saying but im not sure?
I had that n=1 :N before and was able to get one curve but im trying to get the 3 curves with the different step values. with the other for loops.
I keep getting the error "the variable t seems to change size on every loop", I know it technically should if i am trying to use different step sizes.
And I would guess i wouldnt need x1 x2 or x3 to get the 3 different graphs.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Sep 2021
Here is the corrected complete code that computes x1, x2, x3 along with t1, t2, t3 and plots their values accordingly:
a = 0;
b = 30;
x0 = 6;
N = 10;
h = (b-a)/N;
t(1)= a;
x(1) = x0;
for n=1:60
x1(n) = x(n);
t1(n) = t(n);
for n=1:300
x2(n)=x(n);
t2(n)=t(n);
for n=1:3000
x3(n)=x(n);
t3(n)=t(n);
t(n+1)=t(n)+ h;
x(n+1)= x(n)+ h*0.65.*x(n)*(1-(x(n)/5.4));
end
end
end
plot(t1,x1, 'ro-')
hold on
plot(t2,x2, 'g*--')
plot(t3,x3, 'b-')
legend('t_1 vs. x_1 (N=60)', 't_2 vs. x_2 (N=300)', 't_3 vs. x_3 (N=3000)')
xlabel('t_1, t_2, t_3')
ylabel('x_1, x_2, x_3')
grid on
Note that all plot commands are taken outside of the loop that significantly speeds up the simulation.
You should still consider the memory allocation before the loop to speed up the simulation using zeros(), eg. that is a small exercise for you.
Good luck!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!