Why I get error to solve this ODE with euler method?

9 次查看(过去 30 天)
Hello, I need to solve this equation with euler method but I got error in array
This equation is:
dMn(t) dt = Kn-1M1(t)Mn-1(t) - KnM1(t)Mn(t) - μnMn(t)
and my code in MATLAB is
%Metode Euler
%dMn/dt= Kn-1*M1(t)*Mn-1(t)-Kn*M1(t)*Mn(t)-Mun*Mn(t)
clc;clear;
%initial variabel
sigma = 50;
K1 = 10^-4;
K0 = 0.1;
n = 6;
Oa = 10;
Pa = 100;
mui = 10^-3;
muo = 10^-4;
mup = 10^-5;
dt = 0.001;
t = 0:dt:100;
n = zeros(length(t),1);
M(1)= 2;
Mn = 0:6;
Kn = K1*M(t)*M(1)-K1*M(1)*M(t)-mui*M(t);
for i = 1:length(t)-1
K(i+1) = K(i)*M1(i)*Mn(i)-K(i)*M1(i)*Mn(i)-mui(i)*Mn(i) ;
end
and MY ERROR IS
Array indices must be positive integers or logical values.
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-4-6
You are trying to use non-integral values (variable t) as indices to define Kn, which is not allowed.
As the error states array indices must be positive integers.
Are you trying to solve an ODE?
dt = 0.001;
t = 0:dt:100;
Kn = K1*M(t)*M(1)-K1*M(1)*M(t)-mui*M(t);
Array indices must be positive integers or logical values.

请先登录,再进行评论。

回答(1 个)

Luca Ferro
Luca Ferro 2023-4-6
First a suggestion, this line really doesn't make any sense:
M(1)= 2;
just do
M=2;
and you get the same result and you don't have to carry around extra syntax. Still i'm not sure that you wanted M as a double since you use it as an array later.
Then the error is on line:
Kn = K1*M(t)*M(1)-K1*M(1)*M(t)-mui*M(t);
You are trying to access a double (M) as if it was an array with M(t). M has only one element and using an index on it doesn't really work. What's more is that the index that you are using later, dt, is not an integer, meaning that it cannot be resolved anyways. What's the element 0.1 in an array? Positions only go by integers, so 1,2,3,...
Later in the code at line:
K(i+1) = K(i)*M1(i)*Mn(i)-K(i)*M1(i)*Mn(i)-mui(i)*Mn(i) ;
you use a variable that doesn't exist, M1. This will throw an error as well
I would suggest a solution but i'm very confused by what we are trying to achieve.
Also if you could write the equation in LateX it would be better.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by