How can I use for loop to input values when solving ODEs using ode45?

Hi everyone
On the attached code, I am trying to use a for loop to iteratively input values while solving ODEs using ode45. I think I am making a mistake somewhere. I get the error message:
Not enough input arguments.
Error in SlurryCaseODE45Feb14/kinetics (line 31)
[T,Cv]=ode45(@DifEq,t,c0,Options);
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in SlurryCaseODE45Feb14 (line 166)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Please help.

 Risposta accettata

You have to define the initial and final time for the ode45 function rather than just typing t, use [t0 tf].
I suggest editting the function function dC=DifEq(t,c) as it has some missing matrix multiplication operators (.*) and revising the matrix formations that you used. Check line 39 for example.

7 Commenti

Thanks a lot Aara. Line 39 actually gives the CH values I want to input for every iteration.
CH(k) = [1.00E-04;1.74E-05; 3.72E-05; 3.55E-05; 3.55E-05; 1.00E-04; 4.07E-02; 2.45E-01; 6.17E-01; 1.32E+00; 1.32E+00; 2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;1.38E+00;2.09E+00;1.82E+00;1.58E+00;2.29E+00;1.62E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;8.71E-01;1.12E+00;1.00E+00];
I have defined t as tspan, i.e.
c0 = [1e-23;1e-23;1e-23;1e-23;1e-23;1e-23;1e-23];
Options = odeset('RelTol',1,'AbsTol',1);
t=[0
6000
12000
18000
24000
30000
36000
42000
48000
54000
60000
66000
72000
78000
84000
90000
96000
102000
108000
114000
120000
126000
132000
138000
144000
150000
156000
162000
168000
174000
180000];
tspan = linspace(min(t), max(t),31);
[T,Cv]=ode45(@DifEq,tspan,c0,Options);
Please note that this will change the line numbers.I have also subtituted every CH with CH(k), hence the error above was resolved. However, a new error comes up, i.e.
Unable to perform assignment because the left and right sides have a different number of elements.
Error in SlurryCaseODE45Feb16/kinetics/DifEq (line 70)
CH(k) = [1.00E-04;1.74E-05; 3.72E-05; 3.55E-05; 3.55E-05; 1.00E-04; 4.07E-02; 2.45E-01; 6.17E-01; 1.32E+00; 1.32E+00;
2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;1.38E+00;2.09E+00;1.82E+00;1.58E+00;2.29E+00;1.62E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;8.71E-01;1.12E+00;1.00E+00];
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in SlurryCaseODE45Feb16/kinetics (line 62)
[T,Cv]=ode45(@DifEq,tspan,c0,Options);
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in SlurryCaseODE45Feb16 (line 197)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
This has to to with the sizes of matrices in your code, for example you can't do the following:
A = ones(3,4);%matrix with 3 rows and 4 columns;
b = ones(2,5);%matrix with 2 rows and 5 columns;
A = A+b;%attempt to add them...
%this results in an error due to matrix dimensions
The error you get is sort of similar to the one shown above. I suggest you read through the colon notation in matlab documentation to help you resolve this thing. It allows you to set an entire row or column within a matrix in the left hand side equal to the operation you are doing in the right hand side. Look at this for example:
Ch(:,k) = RHS... %this would solve the issue with your code(for that line) but would cause an
%issue elsewhere for the same reason (dimensional inconsistency).
Hope this helps!
Thanks a lot. I think I now understand the cause of the error. I think it because CH(k) is not equal to RHS. The challange is to make them equal.
I have tried,
CH(:,k) = [1.00E-04;1.74E-05; 3.72E-05; 3.55E-05; 3.55E-05; 1.00E-04; 4.07E-02; 2.45E-01; 6.17E-01; 1.32E+00; 1.32E+00; 2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;1.38E+00;2.09E+00;1.82E+00;1.58E+00;2.29E+00;1.62E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;8.71E-01;1.12E+00;1.00E+00];
But I get the error message:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 31-by-1.
Error in SlurryCaseODE45Feb16/kinetics/DifEq (line 70)
CH(:,k) = [1.00E-04;1.74E-05; 3.72E-05; 3.55E-05; 3.55E-05; 1.00E-04; 4.07E-02; 2.45E-01; 6.17E-01; 1.32E+00; 1.32E+00;
2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;1.38E+00;2.09E+00;1.82E+00;1.58E+00;2.29E+00;1.62E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;8.71E-01;1.12E+00;1.00E+00];
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in SlurryCaseODE45Feb16/kinetics (line 62)
[T,Cv]=ode45(@DifEq,tspan,c0,Options);
Error in SlurryCaseODE45Feb16 (line 206)
Cfit = kinetics(theta, tv);
All I want this code do to is to calculate c(1), c(2), c(3), c(4), c(5), c(6), and c(7) at t0, t1, t3 ... tf. As well as to estimate theta(1), theta(2) .... theta(11).
I finally figured out, the code is running.
please share your solution i am also stuck at same

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by