Error in a ODE in a loop (returns a vector of length 9, but the length of initial conditions vector is 2)
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
Hi everybody, and thanks again for the help.
I run the file ciclo_1_bis, which loads from the file z_matlab_1 different vectors, and at every loop
it updates the initial condition, and other parameters stored in z_general_1.
First loop (j=1) ok, the when j=2 it says this erroor message:
Error using odearguments (line 92)
GREITZER returns a vector of length 9, but the length of initial conditions vector is 2. The vector returned by
GREITZER and the initial conditions vector must have the same number of elements.
Error in ode113 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
what could be possibily wrong? It just changes the initial conditions and the time for the next loop.
Accepted Answer
Walter Roberson
on 2 Feb 2021
When you
save main_parameters.mat
after the first loop, then the workspace includes the t that was the result of the [t,y] = ode113() call.
When you load() that .mat inside the function, that t gets pulled inside the workspace, replacing the t that was passed in as a function parameter.
Don't DO that. Do not save() and load() like that. Pass the values in to the function, either individually or as part of a structure. http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
14 Comments
is there a way to stay like that and do something like:
save main_parameters.mat
excluding the t from the first call?
Yes. However it is more secure to explicitly save only the variables you need, and it is more robust to only load the variables you need. It is also more robust to call load() as a function, giving you a struct result with one field per variable, and pulling values out of the struct as needed.
Using save() the way you are is slower and more prone to error.
The changes are not difficult. I will send you the revised version after you answer this:
You have
gamma_T = @(v) A*sin(w*t)+b; % valve's function
which you invoke at gamma_t(t) . What is the point of that function? You do not use v in the body of the function, so the creation of the anonymous function would look up the current values of A, w, t, and b, and "lock them in" to the function handle, causing gamma_T to return the same value each time it was invoked. You then confuse the reader by calling gamma_T(t) which might lead them to think that the (t) value being passed in to gamma_T had some effect on the calculation, that it would become the t mentioned in the body of the anonymous function.. but it will not be. The parameter you pass in to the anonymous function will be ignored.
And since you only call it once, with one value of t, why bother making it a function anyhow? Why not just make it a plain variable?
Also, why do you calculate deltah0c and not use it?
deltah0c:
this code is the result of many coodes I wrote, and maybe I forgot to delete.
gamma_T = @(v) A*sin(w*t)+b; % valve's function
gamma_T was originally a fixed parameter, then I had to make it a function of the time and I was told that was the syntax to make it so.
I think
gamma_T = @(t) A*sin(w*t)+b; % valve's function
should also do fine.
Please verify that
A0=A_eff/.6
and not
A0=A_eff./6
Please verify that in
curva_turbina = A_eff.*((k./R).^0.5).*((1./E_R).^(1/k)).*(((2./k-1).*(1-(1./E_R).^((k-1)./k))).*0.5);
that you do want
and not 
Paul Rogers
on 2 Feb 2021
Edited: Paul Rogers
on 2 Feb 2021
A_eff = .03951;
A_0 = A_eff/0.6;
the one I want is
2/k-1
by writing:
(2./k-1)
At the beginning of ciclo, you load a .mat, and then assign to a number of gas properties such as ro1 . Then you load the .mat again, and assign to gas properties, including a number of the ones you already assigned. Why do you do that? What are the readers to understand if there were a conflict between the numbers used?
Why do you calculate psi_c in both functions? What is the user to understand if there is a conflict between them?
the psi_c valid is the one in the function file, I forgot to comment/delete the one in ciclo.
I loaded that mat file again because the one I am working on now (cicloxxxx) is the result of two different code previously written by me, and I wanted to make sure nothing was missing
I have attached revised versions of the code to illustrate how to change to not use save/load in the function. The greizter_new change is complete, but the ciclo_1_bis_new needs further work to store values into the P structure to be passed to the function. The remaining work is a matter of searching for P. variables used in griezter_new and finding them in ciclo_1_bis_new and verifying that it makes sense to rename them with a P prefix -- which is straight-forward except when it reveals problems such as you assigning to the same variable in both functions, or you assign to the same variable twice in ciclo.
I did not try to dig into why you save and restore the other .mat file, but chances are high that you should not be doing that, at least not repeatedly... maybe once at the end.
how do i save and pass the variables I only need?
just by putting "P" in the function's argumentlike this:
function [ dy ] = greitzer_new( t, y, P )
and adding, for example:
P.kf
P.sigma
and so on?
Paul Rogers
on 3 Feb 2021
Edited: Paul Rogers
on 3 Feb 2021
Walter Roberson thanks a lot, I re-wrote the code the way you told me.
I can't bellive how much I gained in speed! I weent froom almost 1h to a few seconds. Incredible, thanks again for your time.
Now I can focus on the algorythm.
File I/O is a lot slower than computation, except sometimes to SSD.
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
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)