How to plot for a matrix?

1 view (last 30 days)
Westin Messer
Westin Messer on 6 Nov 2018
Answered: Stephen23 on 6 Nov 2018
Hello, in this code I am varying the parameter k from 1 to 4 and I get four different sets of numbers for u and v. I want to plot all sets of these numbers on the same graph so I should get multiple lines. However, when I run it, it only plots for one set of values. Does anyone know how to fix this? Thanks!
%Clear command window and workspace
clear
close all
clc
% Fitzhugh-Nagoma model parameters
e=0.01; k=1:1:4; a=0.1;
i = 0.001;
figure(1);
hold on
u=zeros(500000,4);
v=zeros(500000,4);
t=zeros(100000,1);
% Initial conditions:
u(:)=0.3;
v(:)=0.0;
t(1)=0;
dt=0.001;
%==========================================================================
% Forvard Euler Method, for soluing the ODE
%==========================================================================
for k=1:4
for i=1:1:500000
t(i+1)=t(i)+dt;
% u(i+1) = u(i)+ dt*((1/e)*((k(:)*u(i)*(u(i)-a)*(1-u(i)))-v(i)));
u(i+1,:) = u(i,:)+ dt.*((1/e).*((k.*(u(i,:).*(u(i,:)-a).*(1-u(i,:))))-v(i)));
v(i+1,:) = v(i,:)+ dt*(u(i)-v(i));
end
end
% Getting the phase plot
figure(1);
upts=(-2:.05:2);
unullpts=(k*upts.*(upts-a).*(1-upts));
vnullpts=upts;
plot(upts,unullpts,'black',upts,vnullpts,'black');
xlabel('u'); ylabel('v');
axis([-1 2 -1.5 5]);
plot(u,v,'b')
title('Nullcline Plot')
xlabel('u')
ylabel('v')

Accepted Answer

Stephen23
Stephen23 on 6 Nov 2018
You for to use the index k from the outer loop:
% Fitzhugh-Nagoma model parameters
e = 0.01;
k = 1:1:4;
a = 0.1;
i = 0.001;
u=zeros(5000,4);
v=zeros(5000,4);
t=zeros(1000,1);
% Initial conditions:
u(:)=0.3;
v(:)=0.0;
t(1)=0;
dt=0.001;
%
for k=1:4
for i=1:1:5000
t(i+1)=t(i)+dt;
u(i+1,k) = u(i,k)+ dt.*((1/e).*((k.*(u(i,k).*(u(i,k)-a).*(1-u(i,k))))-v(i)));
v(i+1,k) = v(i,k)+ dt*(u(i,k)-v(i,k));
end % ^ ^ ^ ^ you forgot to use |k|.
end
plot(t,u,'r', t,v,'b')
Gives:

More Answers (0)

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!