Re: Mathematica loop help
- To: mathgroup at smc.vnet.net
- Subject: [mg121167] Re: Mathematica loop help
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 1 Sep 2011 06:04:55 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 8/31/11 at 6:05 AM, anshumanbgg at gmail.com (Anshu) wrote: >I'm new to mathematica and I'm running into trouble with loops. >I have some parameters, say {a,b,c} and some variables, say {x,y}. I >want to solve two non linear equations in x and y using FindRoot for >a range of parameter space, say {a,0,100} and {b,-20,20} and >{c,55,95}, say, all integers and in increments of 1 and store the >solutions in a matrix. varying one parameter at a time so I can keep >track of things. >My FindRoot is spitting out solutions when I run it one by one, >punching in different parameter values but I would like to loop it, >store the solutions and then print it out in a table. >So I wanna do something like this: >While {a,0,100,1} >A[i,j]:= FindRoot[{ f(x,y)==0&& g(x,y)==0},{{x,40},{y,200}}] end >loop There are several issues with the code above which will prevent it from working. First, the syntax for While is wrong. But perhaps this was intentionally not meant to be valid Mathematica syntax. Look up While in the Document Center for correct syntax. Next, you would not want to used DelayedSet (":=") for the purpose you have described. Using DelayedSet will assign a definition for A[i,j] but will not actually evaluate the FindRoot part until you use A[i,j] elsewhere. The syntax A[i,j] in Mathematica means a function named A with arguments i and j. It is not truly an array even though it might be convenient to think of A[i,j] as an element of an array at times. The syntax that would do what you appear to want would be something like sol = Table[FindRoot[{ f[x,y]==0, g[x,y]==0},{{x,40},{y,200}}], {= x,...},{y,...}] Notice, I've made several changes to your call to FindRoot Replace f(x,y} with f[x,y] since I assume you intended f(x,y) to be a function f with arguments x and y. In Mathematica parathesis are used only for grouping. The syntax f(x) is evaluated as the product of a variable f and a variable y. f(x,y) isn't valid f{y,x} would evaluate as {f*x,f*y}. Note, the difference between [], {} and (). These are not interchangeable in Mathematica. The FindRoot syntax for doing simultaneous equations has all of the equations present as a list of equations and does not require the And operator. See the documentation center for FindRoot syntax. I've intentionally omitted i,j from the Table call since these are not ever assigned values in your code. That is if you just corrected the syntax for While and replaced the () with square brackets using f[x,y] etc for your arguments to FindRoot, the loop would have created the same delayed assignment to A[i,j] 101 times. I've also omitted the variable a since it does not appear to be used by your FindRoot call.