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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Solver giving solution for one variable but not for a different but similar variable

PaulN
5-Regular Member

Solver giving solution for one variable but not for a different but similar variable

I have a solver block with an equation and a function

PN_10451271_0-1669741741213.png

Further down the worksheet I define two ranges of values, x and Vb. Both ranges have values in the range 0-20.  g(x) returns values but g(Vb) shows as 

PN_10451271_1-1669741927939.png

I am attaching the worksheet and an Excel workbook which contains the expected results.

I would also like to use WRITEEXCEL to output the values of x and g(x) but I can't get that to work either.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:PaulN)

> I am not sure if I understand you correctly, I was putting this below the solver block

When you wrote "Of your two suggestions, I prefer the second" I thought you were talking about my suggestion of defining a new function gg(Vb):=-log(g(Vb)) below the solve block which defines function g. A function which calls the solve block function g and using NO vectorization. All these functions are setup to deal with single scalar variables and are just later called vectorized with a vector of argument values. It can be seen in the picture I added to that post.

 

> What I thought I could do was add a second constraint like 

You can, but you would have to use the boolean equal  (the fat "=") and because you now have two unknowns, you would have to provide guesses for both and also solve for both, even though you are only interested in on of them.

Solving for both would create a function which returns a 2x1 vector (with H+ and ph) and as you are interested in pH only you would again have to define a further function which calls this solve block function and selects the desired value only. So there is no benefit compared to my suggestion with the follow up function gg(Vb).

 


By the way, is there a way to copy and paste something that you have written into my reply and show that it is a quote from your message?


Yes, but partial quoting is a bit cumbersome. You can use the quote button in the menu of the reply window

Werner_E_0-1669828145126.png

to add the whole post you are replying to and then delete the parts you don't need. Its often easier for me to simply copy and paste the line I'd like to quote and add a "greater than" character in front (which is what I am used to from another forum which does not offer any quoting functionality) as I did in the first line of this reply.

View solution in original post

11 REPLIES 11
PaulN
5-Regular Member
(To:PaulN)

My apologies, the spreadsheet shows pH values instead of [H+] values. Have attached an amended spreadsheet.

LucMeekes
23-Emerald III
(To:PaulN)

By evaluating the ranges you defined for the part of Vb, you've changed the ranges into vectors,

Seems that Prime wants you to vectorise the call to g(Vb):

LucMeekes_0-1669747886548.png

Look up vectorise (in the bottom right corner of the operators, tableau) and see your result.

Then this could be the plot:

LucMeekes_1-1669748057813.png

Success!
Luc

PaulN
5-Regular Member
(To:LucMeekes)

Thank you, Luc. I'm not sure why the vectorisation is necessary but obviously it is.  Anyway, I then used 

PN_10451271_0-1669761317650.png

PN_10451271_1-1669761384814.png

I had to experiment with different guess values in order to get the correct results over the whole range of Vb but 1E-10 seems to work well and I can get a plot like this

PN_10451271_2-1669761974664.png

This is my first Mathcad project so I would appreciate a little more help.  I would like to calculate pH directly rather than solving for H+ and then converting to pH and I am not sure what changes I would need to make in order to accomplish this.

 

 

 

Werner_E
24-Ruby V
(To:PaulN)


This is my first Mathcad project so I would appreciate a little more help.  I would like to calculate pH directly rather than solving for H+ and then converting to pH and I am not sure what changes I would need to make in order to accomplish this.

 


You would have to provide an equation in ph rather than in [H+] for the solve plot to solve.

You may replace every occurrence of [H+] in solve block for 10^(-[H+]).

EDIT: Of course it should read 10^(-pH) and the resulting equation should be solved for pH. Actually it would also work OK if you use 10^(-[H+]) as I had written (mistakenly) because the name of the variable does not matter but of course this would be quite confusing and should not be done.

Or you leave it as it is and define an additional function which gives you pH for provided Vb

Werner_E_0-1669770409566.png

 

 

PaulN
5-Regular Member
(To:Werner_E)

Thank you.  My initially posted worksheet did indeed give many negative results and that was because of a poorly chosen Guess value.  The attached worksheet is correct in so far as it gives the results I expected.

Of your two suggestions, I prefer the second and will test it out tomorrow and report back (it's 01:00 right now and I'm calling it a day ...).

Werner_E
24-Ruby V
(To:PaulN)

I noticed that you wrote about the guess 10^-10 and we get positive values that way and therefore edited my post and added the new screenshot.

I, too, would got for option #2 and simply define a new function for pH which in turn calls the solve block function g.

 

On word (or some more) of explanation because you wrote that you don't understand why you have to use vectorization.

If you use a vector Vb, then the equation in the solve block has a vector on its left hand side and a scalar value (H+ guess is a simple scalar) on the right hand side. Whatever value Prime would chose for H+, the right hand side never will turn into a vector and so Prime can't find a solution.  But if you vectorize the function call, Prime will feed the elements of the vector one by one into the called function and collect the result in vector.

You could (but shouldn't) provide a guess vector H+ and in case of your equation this would even work OK. But it sure is nor a good idea because your function now only works OK if the argument is a vector with exactly 55 elements.

And if your equation contains an expression like H+*H+, it would be interpreted as a vector  product and what you may have intended.

Werner_E_0-1669773118431.png

 

So forget this idea and use vectorization, if the function argument is a vector.

For plotting you could use an undefined variable or a range and doing so would not require vectorization!

Werner_E_1-1669773610065.png

 

PaulN
5-Regular Member
(To:Werner_E)

"I, too, would got for option #2 and simply define a new function for pH which in turn calls the solve block function g."

I am not sure if I understand you correctly, I was putting this below the solver block

PN_10451271_0-1669812321894.png

Is that what you meant?  I also implemented method 1 and changed all occurrences of [H+] in solve block  10^(-[H+]) but I don't really like that approach (just a personal thing).

What I thought I could do was add a second constraint like 

PN_10451271_1-1669812864444.png provide a guess value for pH and then g(Vb) := Find(pH)

By the way, is there a way to copy and paste something that you have written into my reply and show that it is a quote from your message?

Thank you again for all your help.

 

Werner_E
24-Ruby V
(To:PaulN)

> I am not sure if I understand you correctly, I was putting this below the solver block

When you wrote "Of your two suggestions, I prefer the second" I thought you were talking about my suggestion of defining a new function gg(Vb):=-log(g(Vb)) below the solve block which defines function g. A function which calls the solve block function g and using NO vectorization. All these functions are setup to deal with single scalar variables and are just later called vectorized with a vector of argument values. It can be seen in the picture I added to that post.

 

> What I thought I could do was add a second constraint like 

You can, but you would have to use the boolean equal  (the fat "=") and because you now have two unknowns, you would have to provide guesses for both and also solve for both, even though you are only interested in on of them.

Solving for both would create a function which returns a 2x1 vector (with H+ and ph) and as you are interested in pH only you would again have to define a further function which calls this solve block function and selects the desired value only. So there is no benefit compared to my suggestion with the follow up function gg(Vb).

 


By the way, is there a way to copy and paste something that you have written into my reply and show that it is a quote from your message?


Yes, but partial quoting is a bit cumbersome. You can use the quote button in the menu of the reply window

Werner_E_0-1669828145126.png

to add the whole post you are replying to and then delete the parts you don't need. Its often easier for me to simply copy and paste the line I'd like to quote and add a "greater than" character in front (which is what I am used to from another forum which does not offer any quoting functionality) as I did in the first line of this reply.

PaulN
5-Regular Member
(To:Werner_E)

That should have been changed all occurrences of [H+] in solve block to  10^(-pH) - you made the same mistake earlier

Werner_E
24-Ruby V
(To:PaulN)

Yes, I applied a remark in the post you are referring to.

PaulN
5-Regular Member
(To:Werner_E)

Thank you for all your help - it is very much appreciated, and I have learnt a lot from you.

Top Tags