Solving for an unknown, and/or correct call for Findroots?
Newtoigor
I feel bad asking this question but I can't seem to find what I'm looking for in the help topics or on this forum...
Is there a built-in function in Igor that acts in the same way that Excel's solver does? I have a mathmatical function, say: (A^C)+B-C = 0, where A and B are known but C is unknown. Is there a built-in function that will start with a guess of C (user supplied?) and iterate until a value of C is found that satisfies the = 0?
I am trying to get FindRoots to return something useful to me but I keep getting a wrong parameter types. My function code is below:
<br />
function myfun (A1,A2,A3,A4)<br />
Variable A1,A2,A3,A4<br />
Variable myoutput<br />
<br />
return (A1-(A2*exp(A3*A4))-myoutput + myoutput*A3^(A4-A1))<br />
<br />
end<br />
function myfun (A1,A2,A3,A4)<br />
Variable A1,A2,A3,A4<br />
Variable myoutput<br />
<br />
return (A1-(A2*exp(A3*A4))-myoutput + myoutput*A3^(A4-A1))<br />
<br />
end<br />
I have tried writing my function where I pass in a wave with the 4 coefficients (eg function myfun (inputwave)) and making the appropriate substitutions (eg A1 becomes inputwave[0]) but I still get the same error with Findroots.
I am putting in:
<br />
Findroots /L=-100 /H=100 myfun, wave0 //where wave0 is a 4 cell wave of the appropriate parameters in myfun<br />
<br />
or <br />
<br />
Findroots /L=-100 /H=100 myfun, wave0,wave1,.... //where wave0,1,etc are 1 cell waves for the appropriate parameters in myfun<br />
Findroots /L=-100 /H=100 myfun, wave0 //where wave0 is a 4 cell wave of the appropriate parameters in myfun<br />
<br />
or <br />
<br />
Findroots /L=-100 /H=100 myfun, wave0,wave1,.... //where wave0,1,etc are 1 cell waves for the appropriate parameters in myfun<br />
Can anyone explain why I am getting an error "the function myfun has the wrong parameter types or wrong number of parameters?
Thanks for any help!
In any case, if you haven't done it already, you need to look at Help->Getting Started and do at least the first half of the Guided Tour. It won't help with the present question, but it will help you with Igor in general.
So, on to your question...
The function to be used with FindRoots requires a very particular format so that Igor knows how to call it. The format is described in detail in the help. To read about it, copy this command, paste it into Igor's command line and press Enter:
DisplayHelpTopic "Finding Function Roots"
Your description suggests that C is the independent variable; you need a function that implements something of the form
y(C) = A^C + B - C
and then solve for y=0. In the help, it gives this as the format for the object function:
Wave w
Variable x
return <an arithmetic expression>
End
You don't have to use w and x, but the function MUST have a wave first and a variable second. The wave carries in the coefficients of your equation and are not varied during the solution. The variable carries in a value of the independent variable. Igor calls your function with fixed values in the wave, and varies the value of the variable while it looks for a solution.
Your attempts to write the function didn't have the right input format, and that's why FindRoots complained.
That means that w[0] would be A and W[1] would be B, and x would be C. Here is an implementation of your function:
Wave w
Variable C
return w[0]^C + w[1] - C
end
I tested FindRoots with NewToIgorFunc like this:
coefs = {2.5, -10} // These values produce a function with two roots: see attached graph
Make/N=1000/D junk // Make wave for graphing the function
SetScale/I x -15,5,junk // A little experimentation showed this to be a nice range given the coefficients I chose
junk = NewToIgorFunc(coefs, x) // invoke the function to fill the wave with function values
Display junk // make a graph
ModifyGraph zero(left)=1 // Turn on the zero line so you can see where the roots are
Now you're ready for FindRoots. Here is a command:
FindRoots NewToIgorFunc, coefs
which produces this result:
Possible root found at 2.78071
Y value there is -3.31482e-11
So it found one of the roots you can see on the graph. In order to find the other, you need to supply a bracket for the root:
FindRoots/H=0/L=-15 NewToIgorFunc, coefs
Possible root found at -9.9999
Y value there is 3.52694e-10
As a special feature, if your brackets are both positive or both negative, FindRoots will attempt (not necessarily successfully) to find two roots between:
FindRoots/H=5/L=-15 NewToIgorFunc, coefs
Looking for two roots...
Results for first root:
Possible root found at -9.9999
Y value there is 4.29281e-10
Results for second root:
Possible root found at 2.78071
Y value there is 3.05533e-13
One thing you can see from all this is that when finding roots, it is pretty essential to have some rough idea of the shape of your function. The graph is essential!
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
January 20, 2015 at 09:54 am - Permalink