How can I use for loop to input values when solving ODEs using ode45?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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.
Accepted Answer
aara
on 13 Feb 2019
0 votes
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 Comments
Dursman Mchabe
on 13 Feb 2019
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.
aara
on 13 Feb 2019
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).
colon notation link: https://uk.mathworks.com/help/matlab/ref/colon.html
Hope this helps!
Dursman Mchabe
on 13 Feb 2019
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.
Dursman Mchabe
on 13 Feb 2019
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).
Dursman Mchabe
on 14 Feb 2019
I finally figured out, the code is running.
ABHISEK PATI
on 24 Dec 2020
please share your solution i am also stuck at same
Dursman Mchabe
on 26 Dec 2020
Sorry, I misplaced the file.
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)