![]() |
|
|
| Subject:
while loop in Mathematica
Category: Science > Math Asked by: eyal32ny-ga List Price: $4.00 |
Posted:
27 Sep 2004 07:48 PDT
Expires: 27 Oct 2004 07:48 PDT Question ID: 406877 |
I am trying to make a loop in mathematica around:
If [Sign [fc] == Sign[fa], {a = c}, {b = c}]
fa = f[a]
fb = f[b]
c = a + 0.5*(b - a)
fc = f[c]
when I do:
While [n < 10,
If [Sign [fc] == Sign[fa], {a = c}, {b = c}]
fa = f[a]
fb = f[b]
c = a + 0.5*(b - a)
fc = f[c]; n++]
I am getting errors all around:
Set::write: Tag Plus in 0.0222862 +2 is Protected.
Set::write: Tag Times in -0.0112788\2.5 is Protected.
Set::write: Tag Times in -0.0112788\0.110031 is Protected.
General::stop: Further output of Set::write will be suppressed during
this calculation.
what am I doing wrong?
my full code is:
Clear[a]
Clear[b]
Clear[c]
Clear[b]
Clear[f]
f[x_]=(x*E^(-x))-0.16064
a=2
b=3
n=1
fa=f[a]
fb=f[b]
c=a+0.5*(b-a)
fc=f[c]
While [n < 10,
If [Sign [fc] == Sign[fa], {a = c}, {b = c}]
fa = f[a]
fb = f[b]
c = a + 0.5*(b - a)
fc = f[c]; n++] |
|
| There is no answer at this time. |
|
| Subject:
Re: while loop in Mathematica
From: biophysicist-ga on 28 Sep 2004 11:09 PDT |
You need to put semicolons at the end of each command. If you need to see the output, you can put in additional lines like Write["stdout",fa] to look at the values. |
| Subject:
Re: while loop in Mathematica
From: eyal32ny-ga on 28 Sep 2004 12:35 PDT |
I dont think this is the problem.
using :
While[n < 10^2 ,
If[Sign[f[a]] \[Equal] Sign[f[c]],
a = c, b = c;
f[b] = f[c];]
c = a + 0.5*(b - a);
n++;]
works without any problems
removing f[b] = f[c] makes it complain. |
| Subject:
Re: while loop in Mathematica
From: xpertise-ga on 28 Sep 2004 12:46 PDT |
It's hard to guess what you are really trying to do, what's sure that you are attempting some very ugly procedural programming in Mathematica! (I am looking at it) |
| Subject:
Re: while loop in Mathematica
From: xpertise-ga on 28 Sep 2004 12:53 PDT |
well here's your working solution anyway, 4 bucks please :) f[x_]:=(x*E^(-x))-0.16064; a=2; b=3; n=1; c=a+0.5*(b-a); fa=f[a]; fb=f[b]; fc=f[c]; While [n<10, If [Sign[fc]==Sign[fa],a=c,b=c]; fa=f[a]; fb=f[b]; c=a+0.5(b-a); fc=f[c]; n++] |
| Subject:
Re: while loop in Mathematica
From: xpertise-ga on 28 Sep 2004 13:10 PDT |
if you explain me what you really wanna do though I can make the code twice as short at least |
| Subject:
Re: while loop in Mathematica
From: eyal32ny-ga on 28 Sep 2004 15:58 PDT |
the 4$ is yours :) though I think you need to post it as an answer.
what I am trying to do is to find the root of the function between the
points 2,3 using the bisection method.
I have managed to run it without any problems with the following code:
f[x_] :=(x*E^(-x))-0.16064
a=2;
b=3
c=a+0.5*(b-a);
n=0;
While [n<10^2,
If[Sign[f[a]] == Sign[f[c]],
a = c, b = c;
f[b] = f[c];]
c= a+ 0.5 * (b-a);
n++;]
Print[a]
Print[n]
now. when I take out the line : f[b] = f[c];
it complains again that its protected.
Why this line is so important?
more than that when I put the line back I still get the error and the
only way for me to fix it is to copy the whole if statement from the
previous version (the one I didnt remove f[b]=f[c] from ) and
miraclously it works.
HELP ME :) |
| Subject:
Re: while loop in Mathematica
From: xpertise-ga on 28 Sep 2004 16:31 PDT |
f[b] = f[c]; doesn't make sense to me, it could mean your asking to assign 3 to 2 (2=3). If you want to solve equation=0 then just use Solve[] it's 100 times more efficient than some crappy procedural programming. |
| Subject:
Re: while loop in Mathematica
From: xpertise-ga on 28 Sep 2004 16:37 PDT |
Find the complete Mathematica listing to your prob on: http://math.fullerton.edu/mathews/n2003/Web/BisectionMod/BisectionMod.html :) |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |