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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Question on programming in Mathcad

JohnDoe
2-Guest

Question on programming in Mathcad

Hello,

Why b in this example doesn't get assigned?

31-03-2013+14-37-51.jpg

1 ACCEPTED SOLUTION

Accepted Solutions

max is a LOCAL column-vector INSIDE the program! It is then returned in a vector and when the program is invoked and the result assigned a variable the vector is whatever you name that variable.

The reason you get this special result when you try to evaluate max outside of the program is because max and min are also built-in predefined functions of Mathcad. Otherwsie you would get an error as of that variable being unknown.Maybe it would have been less confusing if I named them differently.

See attached for some usage examples.

View solution in original post

20 REPLIES 20

because the variable b you use in your program is a local variable which has no connection to a variable with the same name in the sheet.

The output of your program has not yet been defined and aould then have to be assigned (a function or a variable), in your case you would have to put a "b:=" in front of the program (after specifying for the program output should be).

Ok.

Here I would expect to get a 1 as a value for b. Why do I get two values and why 0.25 and 0.75?

31-03-2013+15-17-03.jpg

Because a function returns what you state in the return statement. If this is missing it returns the last expression being assigned.

In your example r is in the upper half plane so b gets 1, and the last assignment in that branch is that of the range variable l_Umin.

As a with your r is very small, that range begins with approx 0.25, next one is 0.75 and this is already the last, as the next one would be 1.25 which is already larger than the upper limit l/lamda, which is approx. 1.

I think you will have to reconsider the whole program, as I guess you will need not only b but l_Umin, etc. too for later calculatons.

Please keep in mind that l_Umin are NOT vectors but range variables - a big difference. You might consider making them vectors.

Ok. Now I am able to get l_Umax. Any idea how to get l_Umin?

P.S: In the last example I used b as a dummy to check if the branching in the program is working as I expect. What I want to calculate here are just l_Umax and l_Umin.

31-03-2013 15-54-26.jpg

Look at the attached. If you would like a program to return more than one value, you would have to use a vector. In our case a vecor whose elements are the two vectors Umin and Umax.

I made it a function with r and the end value of your vectors as parameters for better flexibility.

Thank you.

I have a question: when I just type "max" I get

31-03-2013+16-41-04.jpg

I thought max is a column vector an we assigned its values row by row in this line

31-03-2013+16-44-23.jpg

so why don't I get a colum vector when typing "max="?

max is a LOCAL column-vector INSIDE the program! It is then returned in a vector and when the program is invoked and the result assigned a variable the vector is whatever you name that variable.

The reason you get this special result when you try to evaluate max outside of the program is because max and min are also built-in predefined functions of Mathcad. Otherwsie you would get an error as of that variable being unknown.Maybe it would have been less confusing if I named them differently.

See attached for some usage examples.

See if the attched will help

Still struggling here.

I tried to display min and max in a row vector but got unexpected result:

31-03-2013+17-03-35.jpg

What is happening here? I don't see any error.

Mathcad is telling you that the outcome is a vector, whose first element is a 2x1 matrix and the second is a 3x1 matrix. I don't work with Prime on a regular basis as it has many disadvantages over Mathcad 15, but obviously Prime defaults to not showing nested matrices. You can change this via the menu - see below.

BTW, you should use the file I posted here http://communities.ptc.com/message/201206#201206 as I changed the variable name, especially that of the function as it would overwrite your frequency.

collapse.png

I noticed one small problem with this expression:

31-03-2013+19-33-47.jpg

It may happen that a + 0.25 is already above end. In this case there is no minimum. But this line adds 0.25 to "a" in all cases and supposes that a minimum must exist.

How can we consider this special case?

Our function returns a vector for lUmin and it has to do so in every case. Thats the reason I had to assign the vectors consisting of 0 each if r is null. The same applies to that special case of yours. You have to return a vector for min. It depends on what you intend to do with those vetcors. If its not appropriate that the value is a+0.25 you have to insert an if-condition if a+0.25>end and set the vector to a value of your choice.

It depends on how you would use that vector later. You even don't have to return a vector for min. So you could later check via "if rows(l.Umin)>0" if a vector was returned or a simple number. You can return a text or a vector with a text as an element. It all depends on how you use the result later and which way you will want to check if ther are minima.

You see the point in this program is that the difference between a maximum and a minimum is always 0.25.

Now there is the case if arg (r)>0 and a=0.25. The first Umax would be at a=0.25 and we add a+0.25 to get the next Umin. However in this sprcial case there exists also a minimum at zero because also 0.25 from Umax.

This is not considered in the line for Umin. One solution is in this special case of a=0.25 to augment the vector of Umin by the first value 0. Do you know how this works?

You mean you want an additional minimum 0 in case of r being a negative real number (argument is pi)? That case was not considered in your first post either, right.

There are probably more elegant ways to to the whole things but its a growing program anyway, so an additional if should do the job.

Where you put that if would depend upon how you would like to handle the case of arg(r)=pi and end<0.25? Want to keep the zero as minimum anyway in this case or error text?

In any case you would use the stack command (augment would do the same thing in horizontal direction)

Umin <-- stack(0,Umin)

which puts a zero as first element of the vector .

It is exact 0.5 because end is already normalized (end is legth of transmission line over wavelegth of signal).

I see a problem with this line:

01-04-2013+13-57-25.jpg

In my actual example end=0.25 and a=0.25 (r = -1 and arg(r) = 3.142 >0). So with my program I detect a voltage maximum at 0.25. Now with the line above we get no minima because a+0.25 > end, but there still exists a minimum at 0.25 - 0.25 = 0.

If we remove this line then we get the old problem again with the automatic minimum at a+0.25. I am thinking of two possible solutions (but still have no clue): working with modulus or a while loop in the form while 0<=Umin<=end Umin<-- a - 0.25 + 0.5*i

You turn every complex number for 180 degree if its inaginary part is negative. I already wondered if thats really what you want or if you ahould not just add 2*pi (instead of pi as you do now) in that case to bring the arg in the range [0 .. 2pi) rather the default (-pi..pi].

But if the turn of 180° really does not matter, why not treat -1 as +1? Or is that the exception from the rule and you need both?

There is quite a difference between the cases r = +1 and r = -1.

In the case r = +1 arg(r) = 0 and |arg(r)|/4*pi = 0. That means according to our program we get the first Umax at zero and the first Umin at 0.25.

In the case r = -1 arg(r) = 3.142 and |arg(r)|/4*pi = 0.25. This means we get the first Umax at 0.25 and we must now look for the next Umin on the line. We know it is 0.25 from Umax but in this case we should substract 0.25 from Umax and not add 0.25 because the first Umin is already at zero.

I tried to eliminate the extra Umin (the automatic one) in an if statement:

01-04-2013+17-04-53.jpg

However I get the error: This function requires at least 5 arguments but is applied to 3.

What argument should I put for the colums? I have only one culumn in my vector Umin.

When trying this:

01-04-2013+17-10-16.jpg

I got the error: You cannot perform an operation on a non existant row or column.

Any idea?

Just a short notice to let you know that the program I supplied has an error as I had not noticed that shifting for pi (for negative arguments) would change min and max. So the values are wrong (changed) for all r's with negative imaginary part. I'll try to clean up later.

According submatrix: Built-in help shoud help, 2nd and 3rd argument are start row, end row, and the last two are start column, end column. You have to provide all of them, even if its a column vector. A routine "subvector" is on the wishlist for a very long time but so far we have to do with selfwritten routines (like the fine collection of Stuart).

See attached a new variant with less if. The two while loops could be consolidated into one, bue I thought it would be clearer that way.

r=0 is now treated as a complex number with argument=0. I guess there will be a check as of the magnitude anyway later in that sheet. If thats not what you want you would have to insert the r=0 check again.

Ah, yes, and of your second question cencerning submatrix. It can't be seen from your pic but I guess your index i is running off from 0. so i-1 is negative but there is no row number -1, so the error is thrown.

Top Tags