What am I doing wrong?
2 次查看(过去 30 天)
显示 更早的评论
% Kinetic Analysis
function dydt = ex2d(t,y)
k1=5.54*10^-3;
k2=2.56*10^-3;
n2=0.061; % n2 is the observed biomass yield for nitrite
n3=0.0229; % n3 is the observed biomass yield for nitrate
dydt=[-k1*y(1)*y(3)
k1*y(1)*y(3)*0.7419-k2*y(2)*y(3)
k1*n3*y(1)*y(3)+k2*n2*y(2)*y(3)];
% y(1) is nitrate
% y(2) is nitrite
% y(3) is biomass
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335169/image.jpeg)
Hello, I;m very confused on what I may be doing wrong here. Maybe it has to do with my format of my matrix but I'm honestly just very confused.
0 个评论
回答(2 个)
Cris LaPierre
2023-3-24
ex2d is a function you have written that requires 2 inputs: t and y. You are trying to run it without supplying the inputs.
Instead, create a script that first defines t and y, and then call your function with those inputs. For example
t = 1:5;
y = sin(2*pi*100*t);
ex2d(t,y)
function dydt = ex2d(t,y)
k1=5.54*10^-3;
k2=2.56*10^-3;
n2=0.061; % n2 is the observed biomass yield for nitrite
n3=0.0229; % n3 is the observed biomass yield for nitrate
dydt=[-k1*y(1)*y(3)
k1*y(1)*y(3)*0.7419-k2*y(2)*y(3)
k1*n3*y(1)*y(3)+k2*n2*y(2)*y(3)];
% y(1) is nitrate
% y(2) is nitrite
% y(3) is biomass
end
0 个评论
John D'Errico
2023-3-24
ex2d is a function. You call a function, even a function you wrote yourself, by passing in arguments. For example, when you use matlab, you can call the function mean. Logically, mean computes the mean of some variable you will pass in. You should understand that. For example, what would you expect to happen here? I'll give a few examples:
x = 1:5;
y = primes(10)
mean(x)
mean(y)
I don't even need to define a variable in advance.
mean(rand(100000,1))
But, if I just type mean, what happens?
mean
Yep. It fails. Why did it fail? Because I never told MATLAB what to take the mean of. What do I want the function mean to operate on. Should it somehow know what I want to to do? (The mind reading toolbox never works for me.)
In your case, you have the function ex2d. It is, again, a function, with arguments. You can pass in anything you want into a function. But you need to pass in two variables here. Internally, the function will see them as t and y, but you can pass in any thing. So the name outside in your calling workspace is not what the function uses internally.
A function is not a script, and I think this is your problem. You are thinking you can use a function just like you used a script.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!