Problem with matlab functions

조회 수: 2 (최근 30일)
Maria
Maria 2011년 4월 5일
Hi all!!
I have a problem with matlab...
I'm trying to solve a ode but before getting to it i'm having trouble defining the function first. The code is the following:
function dXdt = third_simplified_model(~,C)
clc
load Gmatrix;
load Xmatrix;
global k1 k24 k34 k41
i = 750;
if i == 700 const = Gmatrix(:,2); conv = Xmatrix(:,1); elseif i == 750 const = Gmatrix(:,3); conv = Xmatrix(:,2); elseif i == 800 const = Gmatrix(:,4); conv = Xmatrix(:,3); end
const0 = Gmatrix(:,1);
% construction of the function
dXdt = zeros(4,1);
dXdt(1) = k41*C(4) - k1*C(1) - const0(1);
dXdt(2) = - k24*C(2) - const0(2);
dXdt(3) = - k34*C(3) - const0(3);
dXdt(4) = k24*C(2) + k34*C(3) - k41*C(4) - const0(4);
for k1 = 0.1:0.01:50
for k24 = 0.1:0.01:10
for k34 = 0.1:0.01:10
for k41 = 0.1:0.01:50
sol = ode45(@third_simplified_model,tspan,[const(1) const(2) const(3) const(4)]);
end
end
end
end
end
When it comes to the definition of dXdt (1) it gives this error:
??? Input argument "C" is undefined.
Error in ==> third_simplified_model at 35 dXdt(1) = k41*C(4) - k1*C(1) - const0(1);
I've tried several things but nothing works... I don't know if it is from my programming or if it is from the matlab itself because I also tried with the example that the tutorials give to this type of ode solver and it gives exactly the same error:
function dy = rigid(~,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
[T,Y] = ode45(@rigid,[0 12],[0 1 1]);
??? Input argument "y" is undefined.
Error in ==> rigid at 4 dy(1) = y(2) * y(3);
Any suggestions?
Thanks!! =D

채택된 답변

Arnaud Miege
Arnaud Miege 2011년 4월 5일
Make sure your function code is saved in a separate file, third_simplified_model.m. You may want to add an end statement at the end of the file (after the line dXdt(4) = k24*C(2) + k34*C(3) - k41*C(4) - const0(4);).
Then in the MATLAB command window, or in a separate script, run the commands to solve the ODE:
for k1 = 0.1:0.01:50
for k24 = 0.1:0.01:10
for k34 = 0.1:0.01:10
for k41 = 0.1:0.01:50
sol = ode45(@third_simplified_model,tspan,[const(1) const(2) const(3) const(4)]);
end
end
end
end
I have tried it with the rigid example and it works.
Note that with your code as is, you are overwriting the value of the variable sol at each iteration.
HTH,
Arnaud
  댓글 수: 1
Maria
Maria 2011년 4월 6일
Thank you Arnaud! I'll definitely try it. =D

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by