1 view (last 30 days)

Show older comments

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)

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

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!

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

Start Hunting!