필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
cindyawati cindyawati
cindyawati cindyawati 2023년 4월 6일
편집: cindyawati cindyawati 2023년 4월 6일
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.
cindyawati cindyawati
cindyawati cindyawati 2023년 4월 6일
yes, i trying to solve ODE with euler

댓글을 달려면 로그인하십시오.

채택된 답변

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.
  댓글 수: 2
cindyawati cindyawati
cindyawati cindyawati 2023년 4월 6일
편집: cindyawati cindyawati 2023년 4월 6일
this is the equation i want to solve with euler
cindyawati cindyawati
cindyawati cindyawati 2023년 4월 6일
thank you for your respond

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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