Lsqcurvefit errors when trying to optimise
1 view (last 30 days)
Show older comments
Hi, I've been trying to fit a bit of data at a time and I got errors when I tried incorporating fitting an equation of an output of an ODE45 within and now it wouldn't fit any data and I would get errors shown below. Not sure whats happening so any help would be great. I have attached my matlab code here.
Arrays have incompatible sizes for this operation.
Error in lsqcurvefit/objective (line 303)
F = F - YDATA
2 Comments
Answers (1)
Saarthak Gupta
on 27 Dec 2023
Hi Joshua,
It looks like there is a problem with the output of the ODE solver you are using in “kinetics” function, which is the objective function for your curve fitting problem.
While reproducing the problem, the command window in MATLAB gives the following warning: ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1577172/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1577172/image.png)
The solver prematurely terminates at t = 218.2722, and the resulting output is a 12x1 vector. In the current iteration, “lsqcurvefit” uses this vector while calculating the residual, i.e., Y - F(x). The output Y is a 38x1 vector whereas F(x) is 12x1, as calculated by the ODE solver. Hence, it results in a dimension mismatch error.
There can be a couple of reasons for this failure:
1. The error tolerances may be too tight: Variable step-size integrators/solvers adjust the step size based on a weighted norm that evaluates the ratio of the specified error tolerances to the current estimate of local error. If the tolerances are too small, the solver reduces the step size; if they are too large, it increases the step size. However, solvers have a minimum step size limit, and what is happening here is that the result of the error test is to reduce the step size further, but it cannot be reduced because it is already at the minimum allowed size. To fix this, you may loosen the absolute and relative tolerances like so:
options = odeset('RelTol',1e-2,'AbsTol',1e-4);
[~,Cv] = ode45(@(t,C)(InternalODE(t,C)),t,C0, options);
2. The system of differential equations being solved may be stiff: Stiff problems can have rapid changes in solutions over short time intervals. If you suspect that the ODE system is stiff, consider using a solver designed for stiff problems, such as "ode15s".
Refer to the following MATLAB documentation for further reference:
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!