From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

speed up mathscript m file

All,

 

First off, thank you very much for helping me get the program running in my previous post.  Now I come to you because I need to speed up the application.  My ode solver is taking approx 7 seconds to solve.  I need to be closer to 100ms.  Maybe asking too much of the mathscript function, but maybe I am doing something in my code that is not so optimal.  Anyway, can you please consider this code and tell me if there's something faster I can do?  I can't show you all of it, but it's the gist.

 

This m file is used in the mathscript ode_bdf function.

 

Thanks,

Matt

 

function dy = dy(t,y)

% This function provides the dy/dt needed for the ODE solver

dy=y*0.0;                           % Initialize dy/dt vector

phi = zeros(size(y));                % Create phi vector

 

for i=2:5

    if phi(i-1)>=1

           phi(i) = function of y(i) and phi(i-1);

    else

           phi(i) = function of y(i) and phi(i-1);

    end

end

 

for i=1:5

    if phi(i)>=1

        dy(i)=  function of y(i) and phi(i);

    else

        dy(i)= another function of y(i) and phi(i);

    end

end

 

 

0 Kudos
Message 1 of 10
(7,482 Views)

What are typical array sizes?

0 Kudos
Message 2 of 10
(7,480 Views)

five to ten elements.

0 Kudos
Message 3 of 10
(7,475 Views)

There is no obvious reason why this code alone should take 7 seconds to execute, there has to be more to it. Can you show us the rest of the program?

0 Kudos
Message 4 of 10
(7,471 Views)

All that is in the mathscript function is the following:

 

y0=ones(5,1);

options = odeset('Refine',1,'MaxStep',0.5,'NormControl','on','RelTol',1e-3);

[T,Y] = ode_bdf15('y_dy',tspan,y0,options);

 

tspan is fed in as an array from labview, and is [0,1,2,3,4].

 

If I comment out the ode_bdf line, the code is running very fast, 20ms.  Add it in, back up to 7 seconds.  So I am wondering if some time is given to open the m file?  Maybe I'm not initiating the arrays in memory well?

 

Matt

0 Kudos
Message 5 of 10
(7,467 Views)

I forgot to mention that I have a global parameter that is passing several constants in the form of a structure to this m file as well.  Could the global structure be responsible for the slow-down?

 

Matt

0 Kudos
Message 6 of 10
(7,459 Views)

Just saw this while googling:

 

"If you call this function from a MathScript Node, LabVIEW operates with slower run-time performance for the MathScript Node. To optimize the performance of the MathScript Node, remove this function from scripts."

 

source: http://zone.ni.com/reference/en-XX/help/371361G-01/lvtextmath/msfunc_global/

 

this may doom my project if true, the m file must be two inputs, so I had to call this global variable to feed into the m file.

 

Bad day in science.

0 Kudos
Message 7 of 10
(7,454 Views)

So why exactly are you using mathscript at all? Why not use the plain LabVIEW tools?

0 Kudos
Message 8 of 10
(7,449 Views)

I guess the whole story is that one of our math guru's came up with matlab code that works quite well.  It runs really fast if you feed it an array of past data.  Now, I want to run it in realtime for it to be useful on my reactor.

 

So the order of events was supposed to be:

1) matlab code development

2) matlab to mathscript and verification that I get the same as what he gets.

3) modification to mathscript for realtime operation.

 

Since now I realize that mathscript isn't going to work, I guess my next move is to try to figure out how to get labview to solve stiff ode's without mathscript.  Any advice??

 

Thanks,

Matt

 

 

0 Kudos
Message 9 of 10
(7,445 Views)

@I_3_LV wrote:

Since now I realize that mathscript isn't going to work, I guess my next move is to try to figure out how to get labview to solve stiff ode's without mathscript.  Any advice??


I guess I would start here. 😉 (here are some notes about "stiff")

 

 

0 Kudos
Message 10 of 10
(7,438 Views)