Message Boards Message Boards

0
|
2972 Views
|
14 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Error: "Solve was unable to solve the system" after using Rationalize

Posted 2 years ago

Hello, I used Rationalize in Solve, but there is still error:Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

pK0 = Rationalize[-2622.38/T - 0.0178471 T + 15.5873];
pK1 = Rationalize[3404.71/T + 0.032786 T - 14.8435];
pK2 = Rationalize[290239/100/T + 0.02379 T - 6.4980];
pH = -Log[H];
T = Rationalize[t + 27315/100];
Solve[c/b == pK1/H && d/c == pK2/H && 
   b == pK0 a && (a + b + c + c) 12 == DIC, {a, b, c, d}] // Simplify

Can you help me? Thanks.

POSTED BY: Zhenyu Zeng
14 Replies
Posted 2 years ago

NSolve, like some other Mathematica functions, does not assign a value to c when it finds the solution. Instead it returns a list of items that can be used for further calculations.

If you want to assign the value of the solution to c then

c=c/.NSolve[c/b==pK1/H && d/c==pK2/H && b==pK0 a && (a+b+c+d) 12==DIC, {a,b,c,d}][[1]]//Simplify

will do this. You can look up /. in the help system to try to understand how this works.

You can, for example use /. to replace all 1. t with t like this

 c/.(1.t->t)

Mathematica keeps the values of assignments for later use, until you exit Mathematica or tell it to forget an assignment so remember that this happens.

POSTED BY: Bill Nelson
Posted 2 years ago

Hello. What's the meaning of [[1]] in your last code? I felt I can't understand it. May you explain it to me?

POSTED BY: Zhenyu Zeng
Posted 2 years ago

You can use your web browser to look at

help pages

and in the search box in the upper right corner you can type [[ and then tap Enter on your keyboard and it will take you to the documentation for Part. This is usually abbreviated by [[ and ]].

So with a list {a,b,c,d} you can extract the third item with {a,b,c,d}[[3]]

This works when you have lists inside of lists.

If you look at just the result from NSolve you should see that it has {{one solution},{another solution}}

When I just did c=c/.NSolve[...] there was {} around the value assigned to c. I did not want that so I used [[1]] to extract the first item and thus get rid of the {}.

You might find it helpful to quickly read an introduction to Mathematica programming. There are lots of books and even some web pages that will show you some of this. That might make it easier for you to become more productive.

POSTED BY: Bill Nelson
Posted 2 years ago

Thanks. I know now.

POSTED BY: Zhenyu Zeng
Posted 2 years ago

Zhenyu,

solution = 
 NSolve[c/b == pK1/H && d/c == pK2/H && b == pK0 a && (a + b + c + d) 12 == DIC, {a, b, c, d}] // Simplify

(* Solution for a *)
a /. solution

You may be tempted to do this

a = a /. solution

but that is not a good idea because now a has a value and that will change the equations passed to NSolve.

Read the documentation.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thanks. Your method is very good!

POSTED BY: Zhenyu Zeng
Posted 2 years ago

If you replace

pH = -Log[H]

with

H=E^-pH

then Solve will find solutions in terms of pH instead of H. Please check carefully that the signs of H and pH allow this change to be correctly made. I do not know if the form of this solution is what you need. Please carefully test the solutions to make certain that they are correct.

POSTED BY: Bill Nelson
Posted 2 years ago

Hello, thanks for your reply. I found in output there were some 1.t, may can I make it to t not 1.t. Thanks.

POSTED BY: Zhenyu Zeng
Posted 2 years ago

Hello, after these code ran:

pK0 = Rationalize[-2622.38/T - 0.0178471 T + 15.5873, 0];
pK1 = Rationalize[3404.71/T + 0.032786 T - 14.8435, 0];
pK2 = Rationalize[290239/100/T + 0.02379 T - 6.4980, 0];
H = 10^-pH;
c;
T = Rationalize[t + 27315/100];
NSolve[c/b == pK1/H && d/c == pK2/H && 
   b == pK0 a && (a + b + c + d) 12 == DIC, {a, b, c, d}] // Simplify

then I input c, but I can't get the c value but get a c symbol. Why?

POSTED BY: Zhenyu Zeng
Posted 2 years ago

If you display the value of pK0 I believe you will see that it did not completely rationalize and there is still a decimal point in one of the terms. This is probably because of the number of digits after the decimal point in one of the terms. Changing that expression to

pK0=Rationalize[-2622.38/T - 0.0178471 T + 15.5873, 0]

will force the complete rationalization. The documentation for Rationalize explains this.

Then Solve is able to find your solution.

POSTED BY: Bill Nelson
Posted 2 years ago

May you tell me why one of terms has not been rationalized?

POSTED BY: Zhenyu Zeng
Posted 2 years ago
POSTED BY: Zhenyu Zeng
Posted 2 years ago
POSTED BY: Zhenyu Zeng
Posted 2 years ago

Hello, thanks for your reply. Is there a way to show out with pH not H?

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract