ode45 error: must return a column vector

180 visualizzazioni (ultimi 30 giorni)
Hello friends I wrote a code given below, but it give me some errors inclueding "must return a column vector". How could I fix it? The code and errors are given below.
This is my project work due date is tomorrow, so please help me, I need your valuable help
CODE:
clc;
clear all;
mu_Hmax=0.15;
T=22.6;
X_BH=2295;
%
teta=1.25;
Y_H=0.6;
Ks=100;
mu_H=mu_Hmax*(1.072^(T-20));
tm=[30 32 34 36 50 52 57 59 61 63 65 72 75 76 79 83 85 87 89 96 99 106 109 116 118 121 124 125 126 127 131 133 134 135 136 137 140];
%
Sm=[743 585 720 805 571 672 676 574 752 800 535 563 640 800 529 571 588 535 495 641 620 358 515 290 438 356 390 427 483 442 392 429 348 346 356 355 371];
S0 = 1127;
%
Sinr=round(S0*0.5/10)*10:10:round(S0*1.5/10)*10;
tspan=0:140;
%
Ss_in=ones(length(tspan),1)*S0;
res=zeros(length(tm),length(Sinr));
for i=1:length(tm)
%
for j=1:length(Sinr)
Ss_in(tm(i):end)=Sinr(j);
[t Ss]=ode45(@(t,Ss) dynsubstrate(t,Ss,Y_H,Ks,Ss_in),tspan,S0);
res(i,j)=sqrt((Ss(tm(i)+1)-Sm(i))^2);
end
[minres, lmin]=min(res(i,:));
%
Ss_in(tm(i):end)=Sinr(lmin);
end
clear i j
%
Sp=zeros(length(tm),1);
%
for i=1:length(tm)
Sp(i)=Ss(tm(i)+1);
end
clear delta i
delta=-0.25:0.05:0.25;
for i=1:length(delta)
Ksper=Ks+(Ks*delta(i));
[t Ss]=ode45(@(t,Ss) dynsubstrate(t,Ss,Y_H,Ks,Ss_in),tspan,S0);
[t SsKsper]=ode45(@(t,SsKsper) dynsubstrate_Ks(t,Ss,Y_H,Ksper,Ss_in),tspan,S0);
SKsper=abs(((SsKsper-Ss)./Ss).*(Ks./(Ksper-Ks)));
S=max(SKsper(:,1))
end
-----------------------------
ERROR:
Error using odearguments (line 93)
@(T,SSKSPER)DYNSUBSTRATE_KS(T,SS,Y_H,KSPER,SS_IN) must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in DYNTP3P3S3Kspert (line 49)
[t SsKsper]=ode45(@(t,SsKsper) dynsubstrate_Ks(t,Ss,Y_H,Ksper,Ss_in),tspan,S0); % % concentration calculation based on Ksper

Risposta accettata

Steven Lord
Steven Lord il 19 Gen 2021
When you call your function at the initial time and conditions you pass into ode45 does it return a column vector? [I commented this out so MATLAB Answers could evaluate the examples I wrote later in this answer.]
%{
dynsubstrate(tspan(1),S0,Y_H,Ks,Ss_in)
%}
Most commonly this error occurs when the ODE function returns a row vector rather than a column vector. If that's the case preallocate the output using zeros before filling in the elements of it or take the nonconjugate transpose of the output or reshape it or use the A(:) syntax to columnize it.
clear x
y = zeros(5, 1);
for k = 1:5
x(k) = k.^2;
y(k) = k.^2;
end
x
x = 1×5
1 4 9 16 25
y
y = 5×1
1 4 9 16 25
z = reshape(x, [], 1)
z = 5×1
1 4 9 16 25
w = x(:)
w = 5×1
1 4 9 16 25
Note that x is a row while y, z, and w are columns. ode45 would error if your function returned x but could accept y, z, or w as the output.
  5 Commenti
Steven Lord
Steven Lord il 19 Gen 2021
In the third call to ode45 in your code:
[t SsKsper]=ode45(@(t,SsKsper) dynsubstrate_Ks(t,Ss,Y_H,Ksper,Ss_in),tspan,S0);
your ODE function never uses the value of the second input with which ode45 calls that function. Your dynsubstrate_Ks call uses Ss as the second input, which is the solution from the previous ode45 call, but the data that ode45 passes into your anonymous function as the second input is known as SsKsper inside that anonymous function. When I changed the call:
[t SsKsper]=ode45(@(t,SsKsper) dynsubstrate_Ks(t,SsKsper,Y_H,Ksper,Ss_in),tspan,S0);
the call succeeded. I don't know if it gave the correct answers as I don't know what the variable names represent, but it gave some answers.
Carey n'eville
Carey n'eville il 20 Gen 2021
Omg this works, thank you so much thank you!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by