cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Problem passing variables to a function

Frezi
1-Newbie

Problem passing variables to a function

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!

11 REPLIES 11
MikeArmstrong
5-Regular Member
(To:Frezi)

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

Mike

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!

MikeArmstrong
5-Regular Member
(To:Frezi)

Like below?

Clipboard01.jpg

Mike

Perfect, thanks Mike!

MikeArmstrong
5-Regular Member
(To:Frezi)

No problem at all.

Mike

I hope you don't mind but I have another question. I am using a solve block to maximise a variable (j), but it isn't working. I can't tell where the problem lies. Do you have any ideas?

j problem.PNG

Any help would be hugely appreciated - thanks!

MikeArmstrong
5-Regular Member
(To:Frezi)

How about this?

Clipboard01.jpg

Mike

Amazing, thanks again!!

StuartBruff
23-Emerald III
(To:Frezi)

Here's a possible alternative approach ...

collab - aggregate functions 04 A.jpg

(given that Prime has an array row operator, the trick of transposing and passing to a 'columnized' function shouldn't be necessary - just modify ColReduce and ColApply to use the row operator)

Stuart

MikeArmstrong
5-Regular Member
(To:StuartBruff)

As Stuart indicates, Mathcad Prime does have an array operator and i just got my Prime working

Clipboard01.jpg

Mike

MikeArmstrong
5-Regular Member
(To:Frezi)

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

Mike

Announcements

Top Tags