LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with ode45 in Mathscript

Solved!
Go to solution

Hello,

 

I have a non linear model (aircraft) in matlab code .m and I want to use it in labview. I want to see the response of this model and for that I am using the ode_rk45 command of Mathscript. In Matlab it works perfectly but in Labview it takes a long time and at the end it doesn't give you anything. The model has 2 inputs and 20 outputs so maybe it's very big for mathscript. Any idea of how can I solve it?Can anyone help me? I would appreciate a lot an answer. Thank you so much.

0 Kudos
Message 1 of 12
(3,967 Views)

Hi Cris_lbi_1989,

 

Are you able to upload your code for us to take a look at?

 

I would be interested to know what sub function you are calling with the ode_rk45 command and where this function is saved on your system.  It could be that LabVIEW is having a hard time locating the function, which causes the long wait time.

 

When using the ode_rk45 command, you need to ensure that you have set it up correctly using the MathScript node in LabVIEW (LV).  Take a look at the LV help file for this command.  One point of particular interest is that this command will be ignored if you do not request an output, as the function will generate graphical data that is not supported by LabVIEW.

 

Let me know if this helps.

Marshall B
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 2 of 12
(3,955 Views)

Hello Marshall_B,

 

Thank you very much for your answer. I save the .m file in the LabVIEW Data directory, thus I don't need to add any other path. I think I am using well this command like Labview Help says and I don't know what is wrong yet. Sorry, I've seen that I can't attach you de code, could you let me know your e-mail in order to send you the code?. This is the code that I use in Mathscript for ode_rk45:

 

intoptions.AbsTol = 1e-4;
intoptions.RelTol = 1e-4;
intoptions.MaxStep = 1/10;

time=0;

Tf=10;

state=[1.1921 -0.0687 -0.1189 0 0 0 0.0911 0.0605 0.9940 0.9958 -0.0126 -0.0905 0.0071 0.9981 -0.0614 0.0443 6.2712 -0.3858 0 6.2832];

 

while time(end) < Tf

      [timek,statek]=ode_rk45('modelintegacadonew',[time(end) time(end)+dt],state(end,:)',intoptions);

      state = [state;statek];

      time = [time;timek];

 

end;

 

 

Thank you Smiley Happy

0 Kudos
Message 3 of 12
(3,945 Views)

Hi Cris_Ibi_1989,

 

Have you tried attaching your code in ZIP format?  If the file is too large to attach, you could always try sending a sub section of your code or a screen shot for me to look at.  

 

Would I be right in saying that the "modelintegacadonew" function you are calling as part of the command is a .m file which you have created?  I would be grateful if you could send me the .m file that you are using if this is the case.

 

Kind regards,

Marshall B
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 4 of 12
(3,923 Views)

Hi Cris_Ibi_1989,

 

Having had a further look at the MathScript code you sent, I am not sure that the times parameter has been set up correctly.  The syntax for this function is as follows:

 

[t, y] = ode_rk45(fun, times, y0)

 

Looking at your use of this function:

 

[timek,statek]=ode_rk45('modelintegacadonew',[time(end) time(end)+dt],state(end,:smileyhappy:',intoptions)

 

Firstly, I would have expected time(end) to return either an invalid value or a 0 each time, since you have defined this variable as 0 in your code (time=0).  Also, you have not defined a dt value.  I would have thought that this variable needed to be defined beforehand for the function to use it. What time range did you want to use?  It would be better to specify this range within your function, by setting the times parameter to expect a time span of [0, 20] for example.

 

Similarly, what is the parameter state(end) trying to achieve?  

 

Kind regards,

Marshall B
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 5 of 12
(3,917 Views)

Hi Marshall_B,

 

Yes, "modelintegacadonew" is the name of the .m that I call, the model of the aircraft. I send you the code in ZIP format. I hope you can help me and tell me what's going on. Thank you very much.

 

Kind Regards

 

Cris_Ibi_1989

0 Kudos
Message 6 of 12
(3,915 Views)

Hi Cris_Ibi_1989,

 

Thank you for sending me the .m file.

 

Could you also take a look at my previous post and let me know your thoughts?

 

Kind regards,

Marshall B
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 7 of 12
(3,910 Views)

Hi Marshall_B,

 

I defined dt=0.1 but I forgot to copy this line of code here, sorry. I want a time range of 10 seconds and state(end) was actually the whole defined array. Anyway, I've changed the code to make it easier and this is the result:

 

intoptions.AbsTol = 1e-4;
intoptions.RelTol = 1e-4;
intoptions.MaxStep = 1/10;

state = [1.1921 -0.0687 -0.1189 0 0 0 0.0911 0.0605 0.9940 0.9958 -0.0126 -0.0905 0.0071 0.9981 -0.0614 0.0443 6.2712 -0.3858 0 6.2832];


[timek,statek]=ode_rk45('modelintegacadonew',[1 10],state,intoptions)

 

It works perfectly, but it takes about 20 minutes to execute this command. Is this normal?

 

Regards

 

Cris_Ibi_1989

 

0 Kudos
Message 8 of 12
(3,898 Views)
Solution
Accepted by topic author Cris_Ibi_1989

Hi Cris_Ibi_1989,

 

I would have thought so, given the number of steps and iterations being performed.  You are taking 100 steps, based on your step size of 1/10.  The ode_rk45 function performs a fourth order Runge-Kutta numerical method on the equation, meaning that each of these steps will then require at least four iterations.

 

I have seen indications on the Mathworks website that the function can be slow but this can sometimes be down to the way in which the function parameters are being specified and called by the function itself.

 

Let me know if this helps.

 

Regards,

 

 

Marshall B
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 9 of 12
(3,887 Views)

I Marshall_B,

 

Ok, I understand. I proved this program with a better computer and it works faster. It takes some minutes but for me it's enough and I solved my problem. Thank you very much for your help. 🙂

 

Kind Regards

 

Cris_Ibi_1989

0 Kudos
Message 10 of 12
(3,863 Views)