# What am I doing wrong?

3 views (last 30 days)
Vianney Medina Gonzalez on 24 Mar 2023
Answered: John D'Errico on 24 Mar 2023
% 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
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.

Cris LaPierre on 24 Mar 2023
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)
ans = 3×1
1.0e-29 * 0.2218 0.0404 -0.0176
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

John D'Errico on 24 Mar 2023
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)
y = 1×4
2 3 5 7
mean(x)
ans = 3
mean(y)
ans = 4.2500
I don't even need to define a variable in advance.
mean(rand(100000,1))
ans = 0.5006
But, if I just type mean, what happens?
mean
Not enough input arguments.

Error in mean (line 75)
[flag, omitnan] = parseInputs(flag, flag2, isFlag2Set);
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.