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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Shows variable is undefined

fahmed-2
13-Aquamarine

Shows variable is undefined

Hello,

 

I am trying to work with my final solution but Mathcad is saying the variable is undefined even though it's being defined earlier. I have attached the file. Any help will be really appreciated.

 

Thank you,

Faisal

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Werner_E)

Is one of the following two function what you are looking for?

B1.png

or

B2.png

View solution in original post

37 REPLIES 37
LucMeekes
23-Emerald III
(To:fahmed-2)

It's not alpha.1, but psi.ini that is undefined.

Mathcad was never superb at locating exact positions of errors.

 

Success!
Luc

fahmed-2
13-Aquamarine
(To:LucMeekes)

Thank you for your response. What should I do to make it solved

LucMeekes
23-Emerald III
(To:fahmed-2)

Define psi.ini, or take it out of the function, if you don't need it, but I guess you have it in because it serves a purpose. Maybe you are mistaken and meant psi.0 ....?

 

One thing that will help you, is if you take the immediate evaluation off of the end of the definition.

Just define the psi.new function (in terms of K() and the rest), but do NOT put an equals sign at the end.

See if that goes without error. If there are errors in there, solve those first. Then you can evaluate the function.

 

Success!
Luc

Werner_E
25-Diamond I
(To:fahmed-2)

 

B.png

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you for your reply. I was trying to generate a final outcome based on successive increments in an if-else statement but it was saying that it did not converge due to infinite recursion.  How will I approach this.

Werner_E
25-Diamond I
(To:fahmed-2)

Its in no way clear to me what you are trying to do.

 

Some observations:

 

"Lambda" is a vector with just one element - is this whats it supposed to be?

 

"I" is a vector of a couple of values so what do you expect as outcome of your last function? A vector or a scalar.

 

Main problem is that n is a range and so the condition n=0 in your function never is true leading to an infinite recursion.

Werner_E
25-Diamond I
(To:Werner_E)

Is one of the following two function what you are looking for?

B1.png

or

B2.png

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much Werner_E. I wanted to be like this. 

Werner_E
25-Diamond I
(To:fahmed-2)


@fahmed-2 wrote:

Thank you very much Werner_E. I wanted to be like this. 


Hmm - whichever of the two recursive function I provided you are talking of - there is an easier an direct approach for both of them:

B.png

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much for your reply. I am still learning Mathcad and trying to know the syntaxes to make my coding nice and efficient. I liked the first one that you have helped me out. This calculation set was done for one value of Z, is it possible to make it happen for a set of Z's and get segmentwise outputs for each Z. I can make it in a way which might be too long and not great.

Werner_E
25-Diamond I
(To:fahmed-2)

You mean you have a vector of Z-values and each Z-value should calculate a 9 element vector?

What kind of data structure do you expect the result to be? A 9xn matrix or a nested vector?

fahmed-2
13-Aquamarine
(To:Werner_E)

I mean I have a vector of Z-values and each Z-value should calculate an m element vector? Here, m is the number of rows/values in the "I" vector. I need to have an nxm matrix where each column represents one depth

Werner_E
25-Diamond I
(To:Werner_E)

Here is an approach which returns a nested vector

B.png

Worksheet in P4 format attached

fahmed-2
13-Aquamarine
(To:Werner_E)

The (kai subscript "0") should not be constant. It will change the way as you have shown me in the if else loop. I am trying to do this whole calculation set for an n element "Z" vector where the "I" is also an m-element vector.

fahmed-2
13-Aquamarine
(To:fahmed-2)

I can do the whole in only one column vector but I am trying to implement it in different columns where each column represents one depth. I have attached my work

Werner_E
25-Diamond I
(To:fahmed-2)

Are these really the correct values you need?

B1.PNG

 

If yes, then the calculation can be simplified and corrected so the result is a true vector:

B2.PNG

 

Now we can go ahead and create a function which returns a matrix - one column for every value of Z. Again, Z.new should be a vector, not a range. That way the result is a true vector/matrix and we can use it to build a table, export the values, ....

B3.PNG

 

Worksheet in format P5 attached

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much again Werner_E. This works like a magic. I am really thankful. In the final stack, how can I get a value for a particular column and row. For example, if I need to find the value at 3m depth for rainfall of  0.5 mm/hr.  I can do for a single column or, a single row but not at a particular element.

Werner_E
25-Diamond I
(To:fahmed-2)


@fahmed-2 wrote:

Thank you very much again Werner_E. This works like a magic. I am really thankful. In the final stack, how can I get a value for a particular column and row. For example, if I need to find the value at 3m depth for rainfall of  0.5 mm/hr.  I can do for a single column or, a single row but not at a particular element.


Not sure if I understand your problem.

You may use normal matrix indices to access single elements:

B.PNG

Werner_E
25-Diamond I
(To:fahmed-2)

You can even use the table T I created to define an interpolation function so you can evaluate it at arbitrary values for I and Z or plot the 2D-surface.

The routine in the attach uses simple linear interpolation but you may use spline interpolation as well.

B.PNG

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you. My work involves to do a cubic spline interpolation. Should I try with a cubic spline and then use interpolation or, there is a smarter way.

Prime has (among many) a cubic spline function (cspline.)  It will create a function that is an interpolation between knots.

Thank you very much. Should I use as interp(cspline(x,y),x,y,x1)

Werner_E
25-Diamond I
(To:fahmed-2)


@fahmed-2 wrote:

Thank you. My work involves to do a cubic spline interpolation.


You never mentioned that you will be after interpolation so far

 


Should I try with a cubic spline and then use interpolation or, there is a smarter way.

The spline already IS an interpolation!

There is not much you'll have to change if you want to replace my linear interpolation by spline interpolation.

You also may want to know that Mathcad knows three different types of cubic splines (the difference is the behaviour at the end points).

B.png

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much for your email. My work has many pieces and parts and I am going one by one and approaching it. I have been working for almost 3 hours with this problem and might take some more time also. I myself also am trying to come up with various approaches and I truly appreciate your help towards my concern.

fahmed-2
13-Aquamarine
(To:Werner_E)

Is there a way to get the absolute values of a vector. I know the scalar part. 

Werner_E
25-Diamond I
(To:fahmed-2)

One way to do it:

B.png

fahmed-2
13-Aquamarine
(To:Werner_E)

You used try-error statement and mentioned as trying the absolute if not then just return the value. What does this logic signify. And finally, you vectorized the whole to make the output. Can you please tell me why was it done like this. Thank you

Werner_E
25-Diamond I
(To:fahmed-2)

Try and catch was used because your matrix contains a cell with a text (upper left) which would otherwise throw an error when the absolute value is applied.

Vectorization is always the thing to do when you apply calculations on a vector or matrix which is supposed to work not on the whole vector/matrix but on every single element of it. In case of this example you could omit the explicit vectorization because Mathcad will apply automatic vectorization anyway but its a good idea to ALWAYS use explicit vectorization when you want it to be done and don't rely an Mathcads automatic one.

 

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much Werner_E. I am learning a lot from your help

Announcements

Top Tags