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

조회 수: 10 (최근 30일)
Westin Messer
Westin Messer 2018년 11월 24일
편집: Stephen23 2018년 11월 24일
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