LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Mathscript fzero

I am trying to port some pharmacokinetic code from Matlab to Labview. The scripts work perfectly in Matlab, but on Labview, they don't work. The basic script is of the form:

 

function sys = propofol(Age,Height,Weight,Gender)

% Computes STM for Propofol using values from Schnider, et.al.

% Anesthesiology:88(5), 1170-1182

% Time is in minutes.

% Volume in liters.

% Height in cm

% Weight in kg

% Age in years

% Gender is 1 (male), or anything other than 1 (female)

 

if Gender

        lbm = (1.1 *Weight) - 128*((Weight/Height).^2);

else

        lbm = (1.07 *Weight) - 148*((Weight/Height).^2);

end

 

th1 = 4.27;

th2 = 18.9;

th3 = 238.;

th4 = 1.89;

th5 = 1.29;

th6 = .836;

th7 = -.391;

th8 = .0456;

th9 = -.0681;

th10 = .0264;

th11 = -.024;

 

global volume clearance;

volume = [th1 th2+th7*(Age-53) th3];

 

 

clearance=[ th4+(Weight-77)*th8+(lbm-59)*th9+(Height-177)*th10

            th5 + th11*(Age-53)

            th6]';

 

% Solve for the value of ke0 which yields a time to peak effect of 1.6

% minutes.

 

ke0 = fzero('getKe0',.44);

 

[sys] = mam2ss(clearance,volume,ke0);

 

 

function error = getKe0(tke0)

global volume clearance;

tpeak = 1.6;

t=0:.001:4;

u=zeros(size(t));

u(1)=1;

sys = mam2ss(clearance,volume,tke0);

y=lsim(sys,u,t);

tp = t(find(y==max(y),1,'first'));

error = abs(tpeak - tp);

 

 

The getKe0.m script is on the Mathscript path, and gets found, but the fzero returns with the first value I give it. If I use fminbnd, the VI complains

 

"Error -90003 occurred at Error in function find at line 9.  This function is not defined for the number of parameters you supplied.

Macintosh HD:Users:jeffemandel:genModel1:getKe0.m"

 

I can then see that the Mathscript actually invokes a number of VIs which I haven't slogged through, but give me the impression that I probably would do better rewriting all of this in G and not touching Mathscript, except I like having the common codebase between Matlab and Labview.

 

The Matlab routine executes in .2 seconds; the Labview routine takes about 30 seconds, which I could accept if it gave the correct result. 

 

Labview 8.6 Matlab 2008a MacBook Pro OSX 10.5.5 

0 Kudos
Message 1 of 3
(6,582 Views)

Hi jemandel,

The version of your code that calls fzero and the version that calls fminbnd both fail for the same reason: the call to the find function. (The reason they fail in different ways is that there is a bug in the error reporting in fzero. I've filed a bug report to address this). The problem is that the MathScript find function only accepts one input. It chokes on the 1 and 'first' parameters.

I'm not familiar with the syntax for these parameters. Perhaps we can find a way to achieve the same functionality in the 1-input version by manipulating the data before we pass it to the find function. I'm looking into this now.

Regarding performance of MathScript, it is very likely that a pure G implementation of your code would be faster than MathScript. As you noticed, MathScript is built on top of LabVIEW so there will always be some overhead in between that takes time. The fminbnd and fzeros functions you're calling are currently particularly weak in MathScript and we're working hard to improve their performance in future releases of LabVIEW.

jattas

LabVIEW MathScript R&D 

Message Edited by jattas on 09-29-2008 01:37 PM
0 Kudos
Message 2 of 3
(6,568 Views)

I changed the script to:

 

tt=find(y==max(y));

tp = t(tt(1));

 

This works in Matlab, but fails farther along in the call to ss. I'll investigate some more tomorrow. 

0 Kudos
Message 3 of 3
(6,558 Views)