LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Mathscript - modeling Z(x,y)

Although a LabVIEW veteran, I am just starting working with mathscript window (and node).  I did some playing today but need to know the proper way to model uncertainty in a process that I am doing.  Ultimately, the uncertainty (z4) is a function of two variables (twotau and deltat) in the following script - therefore I expect to generate a 3d surface graph with z4, twotau, and deltat on the Z, X, and Y axes, respectively.  Here is the script.  How do I specify the range for twotau and deltat so that this script will run.  Thanks for your help......Don

SR=6.4;
v=0.029;
a=(1/(2*SR));
c=0.02997055434;
z1=-c/(twotau);
z2=c*deltat/(twotau)^2;
z3=sqrt((z1^2)*(a^2)+(z2^2)*(a^2));
z4=(z3/v)*100;


0 Kudos
Message 1 of 16
(10,367 Views)
Based on some of my reading on Mathscript window, I tried the following to specify the range for twotau and deltat. But I still get message of 'unknown symbol on line 5: twotau'

SR=6.4;

v=0.029;

a=(1/(2*SR));

c=0.02997055434;

z1=-c/(twotau(300,350, 50));

z2=c*deltat(5,35, 30)/(twotau(300,350, 50))^2;

z3=sqrt((z1^2)*(a^2)+(z2^2)*(a^2));

z4=(z3/v)*100;


How do I correctly specify the range for twotau and deltat to perform the modeling of z4 as a function of these two variables?

Thanks,

Don
0 Kudos
Message 2 of 16
(10,361 Views)
Hello Don,

You are seeing the unknown symbol error because twotau never appears on the left hand side of any statement in your script.  Thus, it is never defined.  I am not quite sure what is the proper range for twotau and deltat.  Let's assume that you want to define twotau over [-2, 2] and deltat over [0, 100].  One easy way to generate a series of linearly spaced points is to use the linspace command:
twotau = linspace(-2, 2);
deltat = linspace(0, 100);


If you can tell me what range you would like to use, we can ensure the commands work appropriately.  By default, it will generate 100 evenly spaced points.  You mention that your result is composed of two variables and will need to be a surface plot.  In order to set the points correctly, you can use the meshgrid command.  For example,
[xx yy] = meshgrid(twotau, deltat);

Now you can use xx and yy for 2D data where you would have used twotau and deltat, respectively.  Another thing to note is that you will likely want to use scalar operations instead of matrix operations.  For example, when you compute
deltat/twotau
do you really want a matrix division?  Or do you simply want an elementwise division of each element in the matrices?  If you desire the latter, change all *, /, and ^ to have a period in front of them: .*, ./, and .^.

You should then be able to plot your data using the surf command
surf(xx, yy, z4)

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 3 of 16
(10,353 Views)
Hi Grant:

Thanks for getting back to me.  I tried the following but came up with a different error that you might want to comment upon.  I did not for now worry about the matrix vs. non-matrix calculation issue.

SR=6.4;

v=0.029;

a=(1/(2*SR));

c=0.02997055434;

twotau = linspace (300, 350);

deltat = linspace (5, 35);

[xx yy] = meshgrid(twotau, deltat);

z1=-c/twotau;

z2=c*deltat/twotau^2;

z3=sqrt((z1^2)*(a^2)+(z2^2)*(a^2));

z4=(z3/v)*100;

surf(xx, yy, z4);

 

Error in function mrdivide at line 8.  Analysis:  The input sequences must be the same size.


Note that I know I could easily code up what I want in LabVIEW (either straight LabVIEW or formula node + surface graph), but I know how useful it will be to quickly model using mathscript window - so I definitely am excited about this tool and want to learn to use it.

Sincerely,

Don
0 Kudos
Message 4 of 16
(10,349 Views)
ps.  If I add the periods, I now come up with a different error (at least the code made it thru the first 11 lines OK)..You can see by the way the ranges I am interested in for the variables twotau and deltat.  Obviously, I am not a matlab guru and will have to brush up on the syntax rules......Don

SR=6.4;

v=0.029;

a=(1./(2.*SR));

c=0.02997055434;

twotau = linspace (300, 350);

deltat = linspace (5, 35);

[xx yy] = meshgrid(twotau, deltat);

z1=-c./twotau;

z2=c.*deltat./twotau.^2;

z3=sqrt((z1.^2).*(a.^2)+(z2.^2).*(a.^2));

z4=(z3./v).*100;

surf(xx, yy, z4);

 

Error in function surf at line 12.  The sizes of the input matrices are incompatible.  Verify that the matrices have the same size or that one is a scalar.


Message Edited by DonRoth on 04-19-2007 08:35 AM

Message Edited by DonRoth on 04-19-2007 08:41 AM

0 Kudos
Message 5 of 16
(10,351 Views)
Hello Don,

You state in your first reply that you did not concentrate on the matrix versus non-matrix calculations.  Unfortunately, this was the cause of the first error message you received.  Line 8 reads
z1 = -c/twotau;

This tries to divide a scalar by a matrix and it is not a defined operation.  By adding a period to the operation, you tell LabVIEW MathScript to perform a scalar division (i.e. elementwise).  You did add the periods in your second reply and got a different error later in the script.  If you run your script in the MathScript Window and look at the variable values after execution, you can see that we are trying to generate a surface plot with xx and yy (both 100x100 matrices) and z4 (a 1x100 matrix).  These are the incompatible sizes the error is informing you of.  You got off to the right start using the meshgrid function to generate the matrices corresponding to twotau and deltat.  However, you then need to use these matrices in any computation of 2D matrices.  Your script continued to use deltat and twotau in the computation of the z matrices.  Instead of using xx for twotau and yy for deltat, I'll use tt and dt, respectively.  If you change your code to the following, it should work fine.

[tt dt] = meshgrid(twotau, deltat);
z1=-c./tt;
z2=c.*dt./tt.^2;


Of course, you'll also need to change your surf command to be
surf(tt, dt, z4)

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 6 of 16
(10,339 Views)

Yes, this worked fine.  Thanks for helping to get me off the ground on mathscript.

Sincerely,

Don

0 Kudos
Message 7 of 16
(10,335 Views)
A plot is generated that has the X and Y axes reversed from what I want - I see no obvious option to be able to reverse these axes.  I tried to reverse the range numbers in the linspace command but came up with the same result.  Any thoughts on this?

Thanks,

Don
0 Kudos
Message 8 of 16
(10,319 Views)
Don,

By "reversed," I assume you mean you want the numbers displayed in the reverse order, not simply swapping the axes in the plot.  The latter is accomplished by simply changing the plotting command to be
surf(dt, tt, z4);

Unfortunately, there does not seem to be a way to accomplish what you want.  It is possible to implement, of course, and we will consider adding it to a future version of LabVIEW.  You can click and drag the surface to view it from a different direction (and click Tools >> Default View to return to where it started).  In the items menu, you can then turn of the X-Z and Y-Z grid. but I suspect this is not exactly what you want.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 9 of 16
(10,297 Views)

I meant reversing the range on the X and Y axes so all ranges increase from the origin.  So as you said, it does not appear this is possible. I suppose I can go to the mathscript node and then use the surface graph within LabVIEW to do this.  I would think you would want to have this option available - it is more prevalent to consider axes ranges as increasing from a common point.

Sincerely,

 

Don

0 Kudos
Message 10 of 16
(10,295 Views)