Message Boards Message Boards

0
|
5431 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
POSTED BY: hugh trifol
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
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