Surface fit: problems with fit function being too long

I have a fit function that I have created that is well over 400 characters. Igor doesn't allow any line in the definition of the fit function to be more than 400 characters. In the help section, it stated that igor has no line continuation character. So I can't link newlines and can't reduce my function to one line containing less than 400 characters. Anyone know what to do?
You need to break up the long expression into separate pieces and assign them to temporary variables, then write an expression that combines those variables. For instance, an over-simplified case:

Variable term1 = a*x
Variable term2 = b*x^2
return term1 + term2

For a long time, people would send us functions with these horrible, long expressions in them. I was baffled at how anyone could wrangle such a thing. Then I used Mathematica to do some derivatives for me, and I discovered the answer...

I also found that Mathematica isn't terribly good at simplifying expressions. Very often you will find that some group (like, say, x^2-1) will appear repeatedly. That would be a good candiate for pre-computing:

Variable x2m1 = x^2 - 1
return x2m1*w[1] + exp(x2m1...

That can not only shorten your expression, it will speed up the computation, since the exponentiation and subtraction need to be done only once, instead of however many times that group appears in your horrible, long expression.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thank you so much for your quick reply John! I have spent the last couple hours since you posted your response trying to figure out how to make string waves and combine them. My problem now is that when I go to create the new fit funtion under Analysis/CurveFitting I can not figure out how to call my string wave. As a simple example I tried in the command line

make/T 'H' = a*x

then I went to create the fit function. I set the independent variable to x and the parameter to a. Then where it says f(x) =
i don't know what to write and I looked through the help file which in my opinion isn't very good.
I tried f(x)= H and 'H' and "H" and strH and probably a couple others.

As far as the combination of string waves I tried make/T 'A' = string and the same with 'B'. Then I tried make/T 'C' = A + B. Is this the right syntax?

thanks again, Brian
You would write, before the f(x) line,

Wave/T H

Then you can reference H subsequently. But it is highly unusual to reference a text wave in a fit function- I suspect some other problem. Can you describe what you're trying to do?
make/T 'H' = a*x
Did Igor accept that? That looks like you're trying to assign numeric results to a text wave, which won't work. I suspect that you're confused, possibly by the help file... What's the fitting function?

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
My fit function is horrible like you suspected. It is:
1/(4*(x))*(1+(N10 + N11*a + N12*a^2 + N13*a^3 + N14*a^4)/(x)+(N20 + N21*a + N22*a^2 + N23*a^3 + N24*a^4)/(x)^2 + (N30 + N31*a + N32*a^2 + N33*a^3 + N34*a^4)/(x)^3 + (N40 + N41*a + N42*a^2 + N43*a^3 + N44*a^4)/(x)^4 + (N50 + N51*a + N52*a^2 + N53*a^3 + N54*a^4)/(x)^5 + (N60 + N61*a + N62*a^2 + N63*a^3 + N64*a^4)/(x)^6)/(1+(D10 + D11*a + D12*a^2 + D13*a^3 + D14*a^4)/(x)+(D20 + D21*a + D22*a^2 + D23*a^3 + D24*a^4)/(x)^2 + (D30 + D31*a + D32*a^2 + D33*a^3 + D34*a^4)/(x)^3 + (D40 + D41*a + D42*a^2 + D43*a^3 + D44*a^4)/(x)^4 + (D50 + D51*a + D52*a^2 + D53*a^3 + D54*a^4)/(x)^5 + (D60 + D61*a + D62*a^2 + D63*a^3 + D64*a^4)/(x)^6)

I figured that I would make it a series of strings. The first line is stored in A via make/T 'A' = "1/(4*(x))*(1+(N10 + N11*a + N12*a^2 + N13*a^3 + N14*a^4)/(x)+(N20 + N21*a + N22*a^2 + N23*a^3 +". I did this with the rest of the lines and then defined G
make/T 'G' = A + B + C + ... When I appended the outcome to a table it read the whole fit function in its entirety.

Should I not make it a string? In the new fit section it says f(a,x) =
I figure a string of letters and numbers should follow that and the only reason I don't just write out the long string of text is that it says I can only have 400 characters on a line.

Maybe I misunderstand what string means. But Igor wouldn't let me write
make 'A' = 1/(4*(x))*(1+(N10 + N11*a + N12*a^2 + N13*a^3 + N14*a^4)/(x)+(N20 + N21*a + N22*a^2 + N23*a^3 +

I will also explain what I am trying to do if my confusion is unclear, very possible.

I have data points of the form (a,x,z). I suspect these data points can be fit by the horrendous function above. I tried to define this new fit function by using the new fit button. I took all the characters in the fit function and stored it as a string in a string wave G. Now when I am making this new fit function, I am not in a command line, I am in the new window that pops up when you click on the button. I just wanted to make that clear in case there is an easier way to make fit functions through the command line.

thanks for helping to unconfuse me, Brian
btronk wrote:
My fit function is horrible like you suspected. It is:


YIKES!

You have lots of a^n ... You have lots of polynomials in a ... Create lots of variables before the expression ...

    variable a2 = a^2, a3 = a^3, ...
    variable N1 = N10 + N11*a + N12*a2 + N13*a3 + ...
    ...


With such methods, your function might collapse to an equivalent of ...

    retVal = (1/(4*x))*(1+(N1/x) + (N2/x^2) + (N3/x^3) + ...)


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
Brian-

When you put text into the New Fit Function dialog, or if you edit the fit function code in a procedure window, you are writing code. It is not something you can store as a string, it has to be put into a procedure window so that Igor can find it, identify it as code, and compile it. The New Fit Function dialog is just a convenient way to write code that winds up with convenience features for curve fitting.

If you put something in quotes, it becomes a literal string. That is, just a bunch of characters. The compiler doesn't care what's inside a string- that's your business. Code is written without quotes, the compiler pulls it apart bit by bit to parse it and do what it tells Igor to do. It has to follow strict (and sometimes arbitrary) rules.

That whole, bloomin' thing has to be entered into a procedure window as code that Igor understands. I would create the minimum function that you can using the New Fit Function dialog, then edit the result in the Procedure window. You are going to use search, copy, and replace heavily.

Jeff Weimer is right- you should take create variables for all the a^n's, for instance:

Variable ahat3 = a^3

so that Igor only computes it once. You will also need variables to hold bits of the expression:

Variable t1 = (N10 + N11*a + N12*ahat2 + N13*ahat3 + N14*ahat4)

Then at the end compose the overall expression. It will start with:

return 1/(4*(x))*(1+t1 ...

Oh, and the New Fit Function dialog replaces things like w[0] and w[1] that refer to coefficient wave elements with the mnemonic coefficient names you enter in the dialog list. When it generates the code, it makes substitutions into your expression. When you edit the code yourself, you have to make those substitutions.

Good luck!

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thanks so much J.J. and John. I figured out how to write a procedure to fit data, and now all the variable assignations make sense. Both of you have been a huge help.

-Brian