MATLAB Answers

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.

  댓글 수: 0

로그인 to comment.

태그

답변 수: 1

aara 님의 답변 13 Feb 2019
 채택된 답변

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.

  댓글 수: 5

표시 이전 댓글 수: 2
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.

로그인 to comment.



Translated by