Message Boards Message Boards

0
|
4853 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Solve or For loop

Posted 9 years ago

Hi,

How to solve this in Mathematica? What are the ways to do this in an efficient way, For loop might not be the solution, right?

A=6 
B=3 
C=2 
D=1 
E=3.2
pr=1

For i=1 , i++
{

f (pr )=2A pr 6 +Bpr^3 +Cpr^2 +Dpr +E pr^3 

f ’ (pr ) 

prnew=pr-f (pr )/ f ’ (pr )

if (prnew - pr)   <10-3

Break []
else
pr =prnew

}

If no convergence pr=1

Thank you for the help and examples.

Hugh

POSTED BY: hugh trifol
5 Replies
Posted 9 years ago

Patrik,

Works fine and very clear. How can I create a function out of your example so I can reuse it later? Do I need to use Module or With?

[
a = 6;

b = 3;

c = 2;

d = 1;

e = 3.2;

f[x_] := 2 a x^6 + b x^3 + c x^2 + d x + e x^3;

NSolve[f[x]/f'[x] == 0, x]

]

Thank you for your great explanations

Hugh

POSTED BY: hugh trifol

That is very nice to hear Hugh!

That largely depends on what you want to do with the function. From your description, it sound like you want it solve this one problem, that seems a bit limiting, but I'll provide the code below:

solveProblem[] :=
 Module[{a = 6, b = 3, c = 2, d = 1, e = 3.2, f, x},
  f[x_] := 2*a x^6 + b x^3 + c x^2 + d x + e x^3;
  x /. NSolve[f[x] == 0, x, Reals]]

solveProblem[]

{-0.76146, 0.}

You will see that I used a Module. For the parameter values (a, b c..), With will work just fine. When using With, the values specified for a, b, c etc will be directly replaced in the expression, while Module creates local symbols that can be modified later in the expression. For the function f you will want to create local variable that does not interfere with other definitions of f. Also, you might have declared x to have a specific value somewhere else in the code. By creating a local symbol x in the Module, you avoid that conflict.

As I said, it depends largely on what you want to do with the function. If you want to change the parameters you could have them as arguments to your function:

solveProblem[a_, b_, c_, d_, e_] :=
 Module[{f, x},
  f[x_] := 2*a x^6 + b x^3 + c x^2 + d x + e x^3;
  x /. NSolve[f[x] == 0, x, Reals]]

solveProblem[6, 3, 2, 1, 3.2]

{-0.76146, 0}

//Patrik

POSTED BY: Patrik Ekenberg
Posted 9 years ago

Patrik,

thank you for the example. With NSolve how can I select the Real value(s) that are returned, so i can pass this returned value to other functions?

thanks

Hugh

POSTED BY: hugh trifol

Hugh,

you can select the domain by specifying it as the third argument to NSolve:

NSolve[f[x]/f'[x] == 0, x, Reals]

will give you the two real solutions:

{{x -> -0.76146}, {x -> 0.}}

As you can see, what you get out of NSolve is a list of replacements (well, actually a list of a list of replacements, since NSolve can handle multivariate functions). To pass the values to other functions, you could extract the value(s) for example by applying the replacement list(s) to your variable(s):

x /. NSolve[f[x]/f'[x] == 0, x, Reals]

Will give you a list with your solution(s):

{-0.76146, 0.}

You could for example apply every element of the list to a function:

g/@(x /. NSolve[f[x]/f'[x] == 0, x, Reals]) (*equivalent with*)
g[#] & /@ (x /. NSolve[f[x]/f'[x] == 0, x, Reals])

Or if you want to extract one of the solutions manually:

(x /. NSolve[f[x]/f'[x] == 0, x, Reals])[[1]]

Hope that helps!

Patrik

POSTED BY: Patrik Ekenberg

Hi Hugh!

I'm not a 100% sure that I understand what problem you are trying to solve using your for-loop. Are you trying to iteratively solve for a Real x such that f(x)/f'(x) = 0?

If that is the case, you could always use Solve or NSolve:

a = 6;
b = 3;
c = 2;
d = 1;
e = 3.2;
f[x_] := 2 a x^6 + b x^3 + c x^2 + d x + e x^3;
NSolve[f[x]/f'[x] == 0, x]

This will give you the following six solutions:

{{x -> 0.531951 + 0.688517 I}, {x -> 
   0.531951 - 0.688517 I}, {x -> -0.76146}, {x -> -0.151221 + 
    0.34885 I}, {x -> -0.151221 - 0.34885 I}, {x -> 0.}}    

If you absolutely necessarily want to do it with a loop you could write it like this:

maximumIterations = 100;
pr = prnew = -1;
i = 1;
While[i <  maximumIterations,
 prnew = pr - f[pr]/f'[pr]; i++; If[Abs[prnew - pr] < 10^-3, Break[]];
  pr = prnew]
prnew

Which will give you one solution, depending on your start-value for pr:

-0.76146

Hope that helps Hugh. And if not, if you could put your problem in context, I would gladly help you more. Have a nice day!

Patrik

Attachments:
POSTED BY: Patrik Ekenberg
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