cancel
Showing results for
Did you mean:
cancel
Showing results 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

## Parametrize Odesolve and define constants by least sqeare  6-Contributor

## Parametrize Odesolve and define constants by least sqeare

Hello everyone, I am going to parametrize solving block (odesolve func) and then define corresponding coefficients by least square.  But unfortunately, I meet problem after parametrization of the solve block. Could someone help me with this? Thank you in advance. You can find example of the program attached

33 REPLIES 33  24-Ruby V
(To:amusiienko)

Hope that the attached will help.  At the end of the attached file you also find a way to use a solve block with "minerr" to get the optimal value for a.  13-Aquamarine
(To:Werner_E) Viktor

Viktor  6-Contributor
(To:ViktorKorobov)

Thank you very much, it works geat. The last question is about few differential equations. I need to solve a system of diff equations but fit them by the data for only one of the main variables.  For example:

We have  system   p' (t) = a*p -B*p^2 and n' (t) = a*n; with some init cond p(0)=0 and n(0)=1; So I need fit this system by some data from p1 = [10,20,30,40] , t1= [0,1,2,3]. How it is better to do this .

Regards.  21-Topaz II
(To:ViktorKorobov)

Hi Viktor Korobov,

How is that to me the same program does not work? Regards

F. M.  6-Contributor
(To:-MFra-)

Here is an example with a double function. There is some error when I try to use one more equation inside solve block  21-Topaz II
(To:-MFra-)

Ok! It was an oversight ...   6-Contributor
(To:-MFra-)

THis is not a point. I trying add the second equation and Mathcad then does not work. Please have a fast look on my example. It is very similar to yours.  6-Contributor
(To:amusiienko)

Hello everyone,

I am trying to find parameters of the set of differential equations. So, I solve st of dif equations by Odesolve (see attacehed) and then trying to minner coefficients a and Nt1 using experimental data Con and t11.  The system must at least work, but I have error at Minerr step. Could someone help me with this? Thank you, very much.   24-Ruby V
(To:amusiienko)

Obviously the guess values are at a magnitude which makes the odesolve block fail. Changing the guess for a in the minerr block makes the block  work, but the odesolveblock fails using those values:   6-Contributor
(To:Werner_E)

Thank  you, it works now. I went further and meet the next problem.

Minerr function calculates coefficients , but they are negetive some time. How can I give additional condition where all the coefficients must be a positive value. Sorry for obstruction, it look like the last problem which I need to solve.   24-Ruby V
(To:amusiienko)

> How can I give additional condition where all the coefficients must be a positive value.

Simply add statements like Nt1 >0 inside the solve block.

If possible Mathcad will respect it.

You may put more weight in a constraint by doing something like 10^6*Nt1 > 0.  6-Contributor
(To:Werner_E)

Hi everyone, I have developed a program where solve block solves a set of simple equations and then I am trying to minimize parameter value.  1) Parameter Nt is a vector. How can I use minner or least square to get such optimal vector (matrix). 2) I am going to get an optimal solution but there is 2 Input data set Con4(I4) and R(4). The solve block gives us solution let say n(I4) and p(4). I need Con4 = n(I4)+p(I4), and R4= p(I4)-n(4) to be equal. The problem is there is only 1 place for DATA in minnerr func of Mathcad.

Can someone explain how can I use a vector as variable parameter, and then implementing minner(least sqrere) for 2 data sets?   24-Ruby V
(To:amusiienko)

The problem is not minerr!

The problem is that hnu is a vector and not a scalar.

Your last solve block with minerr calls the solve block function Pt (with vector hnu as argument).

Pt calls nt (with vector hnu as argument)

nt calls alph (with vector hnu as argument)

and alph tries to compare the VECTOR hnu against the SCALAR ELD[k

You cannot compare a vector with a scalar.  6-Contributor
(To:Werner_E)

But function Pt() works, please look on nnk=Pt() , the solution of the solve block works fine with no error.  24-Ruby V
(To:amusiienko)

The worksheet being rather unclear and a bit of a mess to me i did not dig deep into the logic of your calculations.

I just tried to change your last solve block into a function with argument hnu which is then called vectorized, but now I got the error that Prime did not found a solution - along with the usual suggestion to change the guess values or the values of TOL and CTOL (which usually does not solve the problem).

Have you checked your solve blocks with simple sample data - do they work OK?

Its usually a good idea to make calculations, especially calcs with solve blocks work OK with scalar arguments, turn them into functions und then call those functions with vector arguments (vectorized). In your case I guess this would be quite some work as in many of your already existing functions you use variables already defined which are not arguments of the function.   6-Contributor
(To:Werner_E)

Sorry for a mess. The starting program was without Minerr, and worked perfectly( just solution of the equations). So as result I can define nnk=Pt(,,,Nt) as a function of Nk. But then I added a minerr block and the minerr block gives error.

It looks like I need rid of I2,hnu,LD,T, for example, redefine some new function F(I,Nt) inside minerr block. But this is the only idea.

Is it possible that miner do not understand vectors as a parametre?  24-Ruby V
(To:amusiienko)

I already wrote above that its not Minerr, throwing the error but the function alph() (and also (alpe()) wher you compare hnu with a scalar ELD[k. As soon as hnu is not a scalar but a vector (as is the case in your sheet), the error is thrown. You can't ask if a vector is larger than 5 - you can only compare a scalar with a scalar.

Nevertheless I guess you can't fix the problem by changing alph aor alpe. You rather have to take care that hnu stays a scalar and is not a vector.

So again: Its not the vector Nt in the solve block which causes the error - its the vector hnu.

Minerr does understand vectors as argument as you can see by the following small example:   6-Contributor
(To:Werner_E)

Hi again,

I prepared a version of the program without complicated functions inside solve block. So now the solution of the system Pt(I,Nt...alphh) is a function of the vectors. But still I have a problem with Minerr and moreover solution as a funk nnaakk=Pt(I,Nt1..) of vector do not work itself. I looked few hours on the program but I can not find something wrong.   24-Ruby V
(To:amusiienko)

The first error I encounter is this  use or   6-Contributor
(To:Werner_E)

Dear Werner,

I have tried this already before and this solution didnt help. See the picture or the program. Maybe someone has ideas about 2nd error?   24-Ruby V
(To:amusiienko)

Additionally there was again a problem with Primes automatic labeling.

Two of the "j" are labeled differently - either label both "automatic" or both "variable"   6-Contributor
(To:Werner_E)

Yes I have done this to transfer "j" parameter to the func nt(I,n,p,j..). This mean that I calculate summarise action for j=0, j= 1, transfer this values in funk nt(t), and then calculate nt() with parameter "j". I had complicated program before where this principle worked. But after simplifying everyshing, i encountered this problem.  24-Ruby V
(To:amusiienko)

???

have you changed as explained above the labels of the two highlighted "j"'s to be the same?

Click on any of them, chose "Labels" on the "Math" ribbon and then set it to same as the other.

Then Pt should evaluate without errors.

The call of nt moans about an incorrect number of arguments then.  6-Contributor
(To:Werner_E)

Thank you!!!! Now I see. How U have found this, it is impossible. You are awesome. IT works now, nut I still have a problem with error 2.  24-Ruby V
(To:amusiienko)
 artem musiienko wrote: Thank you!!!! Now I see. How U have found this, it is impossible

Nearly Experience helped. As the expression was correct and had to work but didn't I remembered that Prime very often fails when trying to autodetect the correct label for a name. This is very nasty as those errors are really hard to spot unless you know what you should be looking for.

 IT works now, nut I still have a problem with error 2.

Prime is right with its error message. I is not defined, so which value would you like I to be?

I replaced I by 10^14.

Next error: You used T1 as a scalar for a result of function Pt and in the next line as a function with argument I1. -> T1(I1) ???

Guessed it was a typo and you meant T1*I1. But Prime seems not to be able to come even close to fulfill the desired equation. Not sure, why. Maybe because your values are around the precision limit of mathcads numerics which is around 15-16 decimals.

BTW. you could also use Here's what i got:   6-Contributor
(To:Werner_E)

Dear Werner , I will explain you what I need. I have data set Con1(I1), where Con1 is conductivity as a function of  Intensity I1.  So I have solved equations in 1st solve block and get p(I) or Pt(I..). So now I need compare Pt(I1) with Con(I1) by Minner in the points I1.  See the last picture in the Mathcad file to see dada.

So I defined T1 as Pt(I,..) and then defined the T1(I1) in points I1 and now trying compare with Con1. SO T1*I1 is wrong it must be T1(I1) and T1 must be a function of "I"  6-Contributor
(To:Werner_E)

Here is example with some solution an experimental data, see  the last picture.  24-Ruby V
(To:amusiienko)

For the next few days or week I will be unable to open Prime files from versions higher than 3.0. Maybe someone else can continue to help you out.

After all "I" was not defined but used in your previous sheet and "T1" was a scalar.

If you want to use a function "T1" (better give it a different name) you would first have to define that function before you can use it.  24-Ruby V
(To:amusiienko)

A last idea - may this works for you (can't check)  