Skip to main content
1-Visitor
July 3, 2019
Solved

Given/find works fine outside but does not work within a loop

  • July 3, 2019
  • 2 replies
  • 3960 views

Hello everyone,

Would really appreciate your help in an issue that I'm facing (in Mathcad 15)

 

I'm trying to fit two curves (parabolas) between two points (could be more later) and certain conditions (slope = 0 at so-so point). 

The given/find equation seems to work fine, but I am not able to loop through it.
The mathcad file is attached. Please allow me to quickly explain what I intend to do
1) I'm defining two parabola as y(x) = Ax^2 + Bx + C and y(x) = Dx^2 + Ex + F

2) Parabola 1 passes through point (x0,y0), with slope = 0
3) Parabola 2 passes through point (x1,y1) with slope = 0
4) They must meet at some point in between (XM,ZM) with the same slopes 

Lastly I find the unknown variables (A,B,C,XM,ZM,D,E,F) and store them in a 8x1 matrix.

But if I need to loop through n points, then I would like to use the same given/find in a loop and store all the calculated uknowns (A,B,C,XM,ZM,D,E,F) in an 8xn matrix. Here I get the error "this variable is undefined" and I have absolutely no idea where I am going wrong.

Many thanks in advance,


Warm Regards,

Aravind.

Best answer by Werner_E

> I was wondering how you were able to call the fun function (which does the find) multiple times without defining the constraints each time

 

The reason is that I turned the whole solve block into a function. So it can be called later over and over again just by using its name "fun".

But be aware that every call uses all the value, guess values, vector I, R.min, etc. with the values they had when the solve block was defined.

If you, lets say, change the value of R.min after the solve block function is defined, this has no effect on the outcome of a call to "fun".

If you would like the solve block function to use different values for R.min at every call, you would have to make R.min an argument of the function, too, alongside with r.

You may even provide different guess values that way if you make a guess value a function argument. You may then delete its definition above the solve if you do so.

 

BTW, you should be very careful with global assignments and use them rather sparingly.

 

Another thing I just noticed: You had defined function f, f' f" twice which is unnecessary. The name of the formal arguments do not matter. You may define f(a,b,c,x):=... and use it later with f(D,E,F,X3)=... as a constraint.

And when you define ff and gg for plotting, you may write ff(x):=f(A,B,C,x) and gg(x):=(D,E,F,x). Less failure prone than repeating typing the same function term over and over again.

2 replies

25-Diamond I
July 3, 2019

A solve block is not supposed to work within a program.

But you can turn a solve block into a function dependent on the variables you'd like to vary and call that function within a program.

Simply close the solve bock with something like

 

fun(x,y,z,..):=Find(....)

 

without an "=" at the end!

x,y,z are the variables you'd like to vary .

 

25-Diamond I
July 3, 2019

See if the attached helps. I made the solve block function dependent on the row index r.

 

BTW, you should consider using units in your sheet! After all you are using Mathcad and being able to deal with units is a big advantage.

 

adnn1-VisitorAuthor
1-Visitor
July 3, 2019

Thanks for the timely help. Your sheet works perfectly  but I am not able to understand why.


I was wondering how you were able to call the fun function (which does the find) multiple times without defining the constraints each time (Given block is defined just once at the top, that too not with global definitions (ctrl + ~)) 

 

Since mathcad follows a top-down approach, it processes the Given, and then after the loop runs first time, it's done processing a find. For the next loop, how does it know the given conditions ?

 

Edit: I completely agree with the units (mathcad's the best at it). But since my sheet is dealing with pure geometry and there is just one unit (metre) throughout the sheet, I skipped the units for now, but will definitely add them once the sheet is working completely.

Werner_E25-Diamond IAnswer
25-Diamond I
July 3, 2019

> I was wondering how you were able to call the fun function (which does the find) multiple times without defining the constraints each time

 

The reason is that I turned the whole solve block into a function. So it can be called later over and over again just by using its name "fun".

But be aware that every call uses all the value, guess values, vector I, R.min, etc. with the values they had when the solve block was defined.

If you, lets say, change the value of R.min after the solve block function is defined, this has no effect on the outcome of a call to "fun".

If you would like the solve block function to use different values for R.min at every call, you would have to make R.min an argument of the function, too, alongside with r.

You may even provide different guess values that way if you make a guess value a function argument. You may then delete its definition above the solve if you do so.

 

BTW, you should be very careful with global assignments and use them rather sparingly.

 

Another thing I just noticed: You had defined function f, f' f" twice which is unnecessary. The name of the formal arguments do not matter. You may define f(a,b,c,x):=... and use it later with f(D,E,F,X3)=... as a constraint.

And when you define ff and gg for plotting, you may write ff(x):=f(A,B,C,x) and gg(x):=(D,E,F,x). Less failure prone than repeating typing the same function term over and over again.