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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

assign a local variable to a global variable without much changes

anthonyQueen
9-Granite

assign a local variable to a global variable without much changes

This is probably a very naive question, from a novice. I am running still Mathcad v.11 from University.

I have a number of equations which use some coefficients. For instance:

1) a:=1, b:=2

2) y:=a*b

3) c:=3*b

4) d==c (read "==" as the three line equality sign, used for global variables)

Step 4) gives me error, because I understood, Mathcad wants that also step 3) and backwards also step 1)b should use "==" global sign.

However, I do not want to change backwards all the steps. Up to now I assign at Step 4) by hand the value of "6", since the variable "c" is not accepted.

Question: Is there a trick to let the value of "c" move to the global variable "d", without changing backwards the other signs?

Many thanks in advance for any help.

16 REPLIES 16

When you run a Mathcad worksheet it first runs through the whole sheet dealing with global variables. It then returns to the beginning and runs through dealing with local variables, so the local variables are effectively non-existent when global variables are being assigned. Why do you want to do this anyway? If you explain what you are trying to achieve by doing this, we might be able to provide something you could use.

Alan

Thanks Alan and Kohlhepp (just below). Let me explain further to Alan (in fact the use is in the same lines of Kohlhepp's message).

a) I have a matrix of coefficients, A, which do not normally change -- they do only in very rare occasion (structural changes, not ordinary ones)

b) Then I have a vector of quantities, Q, -- this vector makes ordinary changes.

c) Some initial results are obtained with A and Q.

d) There are some manipulations of A, and then it is calculated the dominant eigenvalue/vector of this manipulations all based of the matrix A.

e) The vector Q multiplies the manipulated square matrix A and generates a rather wide set of results.

Now in one of the substeps of d), I extracted the dominant eigenvalue and eigenvector of (I-A)^-1. To do this all the assignments where done with ".=" and then the dominant egenvector where scaled further so that the sum of its individual elements was equal to the sum of the elements of the "real" vector Q (of step b).

So far so good. Now the issue: I wanted to show what happens to the results c) - e) if I take the values of the eigenvector (appropriately scaled) and assign this vector to the vector of the quantities Q. This is just for a checking.

Supposing that the real quantities, Q are (10 20), and the dominant eigenvector Q_star is (1 29), up to now I had step b) written as follows:

Q:= Q_star [and below at step d) in the sheet I had Q_star==(1 29) written manually, since I do not want to touch the rest of d) and above it]

Q:=(10 20)

when I wanted to check what happens with the dominant eigenvector, I just add an underscore to the "real" Q.

For instance:

Q:Q_star

_Q:(10 20) [where _Q is of no use below in the sheet]

In this way things are simple, and they work. But the drawback is that if matrix A, at step a) changes I have to retype manually Q_Star==.......... at step d). And I have to remember to do this!

------------------

I was looking just a command that "types" the new eigenvectors for me in the global variable Q_star, without any further changes. Re-check backwards the worksheet, in the attempt to change ":=" with "==" would be for me impossibile - a real headache. I also tried with two lines of programming but with no avail (I was even thinking to pass the result to excel and then re-import it, in the attempt to make the automatization possible!).

I hope to have clarified in some way the logic of what I am doing, or to say better, wanted to do.

Thanks again for your help.

Anthony

anthony Queen wrote:

Thanks Alan and Kohlhepp (just below). Let me explain further to Alan (in fact the use is in the same lines of Kohlhepp's message).

a) I have a matrix of coefficients, A, which do not normally change -- they do only in very rare occasion (structural changes, not ordinary ones)

b) Then I have a vector of quantities, Q, -- this vector makes ordinary changes.

c) Some initial results are obtained with A and Q.

d) There are some manipulations of A, and then it is calculated the dominant eigenvalue/vector of this manipulations all based of the matrix A.

e) The vector Q multiplies the manipulated square matrix A and generates a rather wide set of results.

Now in one of the substeps of d), I extracted the dominant eigenvalue and eigenvector of (I-A)^-1. To do this all the assignments where done with ".=" and then the dominant egenvector where scaled further so that the sum of its individual elements was equal to the sum of the elements of the "real" vector Q (of step b).

So far so good. Now the issue: I wanted to show what happens to the results c) - e) if I take the values of the eigenvector (appropriately scaled) and assign this vector to the vector of the quantities Q. This is just for a checking.

Supposing that the real quantities, Q are (10 20), and the dominant eigenvector Q_star is (1 29), up to now I had step b) written as follows:

Q:= Q_star [and below at step d) in the sheet I had Q_star==(1 29) written manually, since I do not want to touch the rest of d) and above it]

Q:=(10 20)

when I wanted to check what happens with the dominant eigenvector, I just add an underscore to the "real" Q.

For instance:

Q:Q_star

_Q:(10 20) [where _Q is of no use below in the sheet]

In this way things are simple, and they work. But the drawback is that if matrix A, at step a) changes I have to retype manually Q_Star==.......... at step d). And I have to remember to do this!

------------------

I was looking just a command that "types" the new eigenvectors for me in the global variable Q_star, without any further changes. Re-check backwards the worksheet, in the attempt to change ":=" with "==" would be for me impossibile - a real headache. I also tried with two lines of programming but with no avail (I was even thinking to pass the result to excel and then re-import it, in the attempt to make the automatization possible!).

I hope to have clarified in some way the logic of what I am doing, or to say better, wanted to do.

Thanks again for your help.

Anthony

I suggest you provide a worksheet using these steps as far as possible and pointing out where you include the manual assignment that you'd like to eliminate. Otherwise we could spend a long time iterating from what we think you want to what you actually want (I'm sure the above explanation is perfectly clear to you, but my poor brain could do with some clarification!)

Alan

So far so good. Now the issue: I wanted to show what happens to the results c) - e) if I take the values of the eigenvector (appropriately scaled) and assign this vector to the vector of the quantities Q. This is just for a checking.

You need to write c)-e) as functions of Q, rather that just simple assignments. If I write

a::2

b::3

c=a*b

and then want to change a and/or b I need to change the original assigment(s). But if I write

c(a,b):=a*b

I can call the function anywhere later in the worksheet, with different values of a and/or b.

The short answer is "No!"

As Alan explained, global variables are permanently assigned. Local assignments "c:=2" only exist below and to the right of the assignment statement. If (further down the sheet) you type "c:=4" then above that statement c = 2, and below that statement c = 4. If you had these two statements, and typed

"d==2*c" what would the value of "d" be? Would d = 4, or would d = 8? Mathcad can't deal with the confusion, so it requires global variables to be permanently defined.

The only real use I've found for global variables is where you have a result that you want to manipulate by changing an input variable that was initially set four pages up the sheet. A global definition next to the graph will allow you to change the input value without scrolling up and down the document; saves wear and tear on the mouse!

Fred

My simple logic would say: take the last value of c --the one that holds just above the global assignment. But I understood that Mathcad, on this point, follows a logic different from mine. For this reason I was looking some code that accomplishes a task like "print text to the (global) variable taking it from a local variable, without asking further".

Anthony

Why do you want d to be globally assigned though? Why do you think a local assignment will not work? What are you intending to do with d once it's assigned?

Because, as I tried to explain above, I need to use the result that comes later, at the very beginning of the worksheet.

I thought that the only way to see the result at the very beginning was to assign a global variable to it. I do not know another way to do this.

But as Alan as said, the problem here is that Mathcad looks first at global and then at local. So if global is even *indirectly* dependent on local variables, it gives error.

I guess that this was done to avoid any possibility of infinite looping in the worksheet. Other (different) programs like Vissim (which I name because it allows,

quite beatifully to use mathcad worksheets as objects), does use global variables without restrictions. And as far as I understand, it makes a finite number of loops (to be chosen in the settings) when it runs.

Just a suggestion: probably it would be nice to have "semi-global" variables, that are read after local, but that are available throughout the worksheet.

Anthony

Mathcad does not provide a function, but it does provide a way.

As I understand it, you want the worksheet to interate so that a result at the end of a set of calculations is in agreement with the intiail value assigned to the variable (or somthing like that).

so:

Block head way: copy the complete set of calculaitons below the first set; Make the first set of calculations an area, including the intial definition of the variable, and then collapse the area, so that you only see the initial variables and the calculated variable as the domain for the second go-though. OK for one interation, but can do for more.

Better way, just program the whole thing and iterate inside the program, or if you want intermediate results and some control, iterate the program by just calling it again, and again, or what ever.

ps

I can only imagin the damage I could reek if I had a globally defined recursive variable.

RichardJ
19-Tanzanite
(To:wayne)

I can only imagin the damage I could reek if I had a globally defined recursive variable.

You can sort of do that actually, if you write a scripted component that uses the GetValue method to get a value rfrom the end of the worksheet, and assignes that value to it's output at the top of the worksheet. Exactly what happens when you do that depends on the version of Mathcad and what you are doding in the middle of the worksheet, but it's usually pretty ugly

Thanks so far to all suggestions, and opinions. You learn a lot from other's advices.

In the version I have (v.11) there is no function "GetValue" as far as the manual tells me.

Yes, I am aware of the possibility (that I partially used) of copy/past the upper part of the worksheet to re-iterate below the calculations with the new values.

and yes I can understand that I could make a very long line of programming and run the new calculations inside it.

I did not fully get the suggestion of Richard c(a,b):=a*b etc. But I will study it further.

I was just inquiring if there were other tricks of the trade to make use of.

Thanks again.

Anthony

MikeArmstrong
5-Regular Member
(To:anthonyQueen)

In the version I have (v.11) there is no function "GetValue" as far as the manual tells me.


If I'm not mistaken I think Richard is referring to a scripted component and using the "GetValue" function in that.

Mike

Thanks again, but as a novice, I just vaguely captured a tiny bit of what you said, apart the fact that it could be interesting !

In the version I have (v.11) there is no function "GetValue" as far as the manual tells me.

It's part of the automation interface, and described in the developers reference. It was a comment to Wayne, and is not a solution for what you want to do.

I did not fully get the suggestion of Richard c(a,b):=a*b etc. But I will study it further.

As I understand it, the attached worksheet gives a very basic example of your problem. Assuming that I did understand correctly, it shows how to address it using functions.

Thanks Richard, for the prompt reply and helpful file. I shall study it carefully!
Anthony

MikeArmstrong
5-Regular Member
(To:RichardJ)

Nicely explained Richard.

These types of worksheets offer much more than just an answer. When I was first learning Mathcad and searching through 1000's of threads, the worksheets with descriptive text helped so much more.

Mike

Top Tags