Error: Not enough input arguments

6 Ansichten (letzte 30 Tage)
Laureana
Laureana am 21 Sep. 2022
Kommentiert: Laureana am 22 Sep. 2022
Hi! I'm trying to run the command from https://la.mathworks.com/help/optim/ug/fit-ode-problem-based-least-squares.html and I have a problem when I run the script:
>> diffun
Not enough input arguments.
Error in diffun (line 3)
s12 = y(1)*y(2);
function dydt = diffun(~,y,r)
dydt = zeros(6,1);
s12 = y(1)*y(2);
s34 = y(3)*y(4);
dydt(1) = -r(1)*s12;
dydt(2) = -r(1)*s12;
dydt(3) = -r(2)*s34 + r(1)*s12 - r(3)*s34;
dydt(4) = -r(2)*s34 - r(3)*s34;
dydt(5) = r(2)*s34;
dydt(6) = r(3)*s34;
r = [2.5 1.2 0.45];
y0 = [1 1 0 1 0 0];
tspan = linspace(0,5);
soltrue = ode45(@(t,y)diffun(t,y,r),tspan,y0);
yvalstrue = deval(soltrue,tspan);
for i = 1:6
subplot(3,2,i)
plot(tspan,yvalstrue(i,:))
title(['y(',num2str(i),')'])
end
end
I'm new to using matlab, I'd appreciate any help.
Thanks!

Akzeptierte Antwort

Torsten
Torsten am 21 Sep. 2022
r = [2.5 1.2 0.45];
y0 = [1 1 0 1 0 0];
tspan = linspace(0,5);
soltrue = ode45(@(t,y)diffun(t,y,r),tspan,y0);
yvalstrue = deval(soltrue,tspan);
for i = 1:6
subplot(3,2,i)
plot(tspan,yvalstrue(i,:))
title(['y(',num2str(i),')'])
end
function dydt = diffun(~,y,r)
dydt = zeros(6,1);
s12 = y(1)*y(2);
s34 = y(3)*y(4);
dydt(1) = -r(1)*s12;
dydt(2) = -r(1)*s12;
dydt(3) = -r(2)*s34 + r(1)*s12 - r(3)*s34;
dydt(4) = -r(2)*s34 - r(3)*s34;
dydt(5) = r(2)*s34;
dydt(6) = r(3)*s34;
end
  1 Kommentar
Laureana
Laureana am 22 Sep. 2022
Thanks @Torsten! I just had to change the name of the function because it was the same in the script and it works!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by