How to correctly change a variable from a single number to an array.

7 次查看(过去 30 天)
Hello, I oringinally wrote this matlab code to model the FitzHugh-Nagumo action potential. Now I'm trying to modify the model to get a string of action potentials. To do this I need to change all the state variables to array shown below:
These equations:
% Fitzhugh-Nagoma model parameters
e=0.03; k=3; a=0.05;
Are changed to this:
% Fitzhugh-Nagoma model parameters
e=[0.03 0.03]; k=[3 3]; a=[0.05 0.05];
However when I do this the code doesn't run. Can somebody explain what needs to change in the rest of the code to get it to work? Thanks a lot!
%Clear command window and workspace
clear
close all
clc
% Fitzhugh-Nagoma model parameters
e=[0.03 0.03]; k=[3 3]; a=[0.05 0.05];
i = 0.001;
figure(1);
hold on
u=zeros(100000,1);
v=zeros(100000,1);
t=zeros(100000,1);
% Initial conditions:
u(1)=0.6;
v(1)=0.0;
t(1)=0;
dt=0.001;
%==========================================================================
% Forvard Euler Method, for soluing the ODE
%==========================================================================
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)));
v(i+1) = v(i)+ dt*(u(i)-v(i));
end
% Getting the plot
figure(1);
plot(t,u)
legend('u','Trajectory')
title('Time Series Plot')
xlabel('Time')
ylabel('u')
xlim([0 5])

采纳的回答

Jan
Jan 2018-11-24
编辑:Jan 2018-11-24
e = [0.03 0.035]; k = [3 3]; a = [0.05 0.05];
n = 500000;
u = zeros(n, 2);
v = zeros(n, 2);
t = zeros(n, 1);
% Initial conditions:
u(1, :) = 0.6;
v(1, :) = 0.0;
t(1) = 0;
dt = 0.00001;
% Forward Euler Method, for soluing the ODE
for i = 1:n - 1
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, :)));
v(i+1, :) = v(i, :) + dt * (u(i, :) - v(i, :));
end
figure;
plot(t, u)
All you have to do is to define u and v as matrices instead of vectors and to change \ and * to the elementwise operations .\ and .* . Then acces the subvectors as u(i, :) instead of u(i).

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by