Skip to main content
1-Visitor
April 20, 2012
Question

Problem passing variables to a function

  • April 20, 2012
  • 2 replies
  • 5549 views

Hello,

I'm using MathCad Prime 2.0 and am pretty new to the software but am having trouble passing variables as arguments to functions.

I am trying to use a For loop within my function and don't think I am doing anything wrong in terms of syntax because even if I just copy the For loop example in the tutorial I don't get the answer shown in the tutorial. I have attached a screenshot of the problem but it's also typed below if that helps.

mathcad.png

In other words, the result I get is as though the 'vec' array has not be read. The answer I would expect would be a column vector containing 4 items, all zeros except the fourth, which would be a 7. Has anyone else had this problem? I can't work out what I'm doing wrong.

On a similar example the variables I am trying to assign as arguments to a function result in the error message 'The variable is undefined. Check that the label is set correctly'. I'm pretty sure that this is because the arguments are not passing to the function rather than a labelling problem.

Any help would be hugely appreciated!

Thanks!

2 replies

1-Visitor
April 20, 2012

Firstly you don't need to define vec above the function and you should evaluate f(vec) below.

Mike

Frezi1-VisitorAuthor
1-Visitor
April 20, 2012

Thanks Mike - I've managed to get this working now. I now have one more question which hopefully you won't mind answering!

I have two column vectors and I would like to find the minimum for each row (i.e. minimum of row 1 for vector 1 and 2, then min of row 2 of both vectors etc), writing the values to a new column vector. It seems like a simple thing to do but I can't work out a way to do this other than shown below. However, it doesn't seem to be working as my function seems to only return the overall minimu of the matrix rather than the minimum of each row. I hope this is making sense!

minimum.PNG

Whereas the answer I am looking for is Test(B)=[1, 1, 4, 7, 5, 2] (as a column vector)

I can't see what I am doing wrong! I was unable to convert your worksheet, if you have an example would you mind attaching a screenshot?

Thanks!

1-Visitor
April 20, 2012

Like below?

Clipboard01.jpg

Mike

1-Visitor
April 20, 2012

Example in M15 attached Sorry I haven't got Prime at the moment, so you will have to convert it.

Mike