Runtime Errors: MatLab Optimisation

Hi everyone! I am trying to run an optimisation problem using fmincon which involves numerically solving an associated system of differential equations as in the code attached. Currently, running the code returns a few errors that I can't figure out. These are the errors that the compiler prompts:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in newcrime>myode (line 74)
K1 = K(:,1:N);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in newcrime (line 38)
[t,z] = ode15s(@myode, [0,tf], z1, opts, K);
I have pursued some options with changing function parameters and redefining local paramters within the functions, all to no avail. Any help would be really appreciated! Thank you!

Risposte (1)

Walter Roberson
Walter Roberson il 6 Mag 2019

0 voti

The line of code giving the error in the error message does not exist inside the code that you have posted.
The code that you have posted appears to use K correctly. You build a row vector x, use it to calculate some values, and use .' to transpose that into a column vector, and you put two of those together into a column vector, so K is a single column vector. It is incorrect to try to access multiple columns of it.
You should not be using that syntax to pass K into the ode function. See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html showing how to pass it in through the function handle.

1 Commento

Hi Walter, thanks for your response. It's just that when I pass K into the ode function without setting up the events function and the optimisation problem, it does work. I did realise one thing though and that is that I didn't need to access multiple rows of K -- i thus changed K[:,1:N] to K[1:N] everywhere in my code. However, the system returns the same error. I'm not sure I understood all you were trying to say, but was this getting at it? If not, could you please elaborate on your advice?
Once again, thanks so much for your help! I really appreciate it!
Attached is the slightly updated code.

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Prodotti

Release

R2019a

Richiesto:

il 5 Mag 2019

Commentato:

il 6 Mag 2019

Community Treasure Hunt

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

Start Hunting!

Translated by