Message Boards Message Boards

0
|
6193 Views
|
3 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Solve system of non-polynomial eqs with numerical parameters with FindRoot?

I am very new to Mathematica. I need to solve numerically a system of two non-polynomial equations. I've found that one should use FindRoot function for that. However, I have a problem with FindRoot because it seems not to accept numerical parameters - all parameters must be entered as numbers, not as constants, otherwise the function returns an error "The function value ... is not a list of numbers with dimensions {2} ".

However in my case I will need to solve the same system of equations for hundreds of times, each time with different values of some numerical parameters. The only way I can perceive doing this is to define those parameters outside the FindRoot function (like for example K = 5, where K is a parameter, and then I should use letter K inside FindRoot function). But FindRoot does not accept that. What is the solution?

POSTED BY: Lev Dorosinskiy
3 Replies

Thank you for your suggestions. They gave me some idea but there is still one thing that I can't figure out. In the examples you gave the values of k are entered explicitly by hand. But in my case the parameter k (in fact, not just one k but 4 different parameters k1, k2, k3, k4) will be read from file. And then I will need to solve the system of equations for each combination of the values of these 4 parameters. That will be done hundreds of times. In that case, according to my understanding, I will need to have a cycle, in each step of the cycle k1...k4 will be defined but in the expression to be solved they must be entered as symbols then, not as fixed numbers. How can that be done?

POSTED BY: Lev Dorosinskiy

I would advise one small change:

sol[k_?NumericQ] := FindRoot[Sin[x] + Exp[x] == k, {x, 0}];

Or even further

ClearAll[sol];
sol[k_?NumericQ] := Module[{x}, x /. FindRoot[Sin[x] + Exp[x] == k, {x, 0}]];

Then one can use sol[k] in other functions:

NIntegrate[sol[k], {k, 0, 1}]
(*  -0.276615  *)

Alternatively, one can use Block:

Block[{k = 3}, FindRoot[Sin[x] + Exp[x] == k, {x, 0}]]
(*  {x -> 0.819443}  *)
POSTED BY: Michael Rogers

You can use delayed evaluation:

sol[k_] := FindRoot[Sin[x] + Exp[x] == k, {x, 0}];
sol[0]
sol[1]

This way the numerical algorithms of FindRoot will be called only after a value has been decided forĀ k.

POSTED BY: Gianluca Gorni
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