Creating function from Matlab cftool fit? - matlab

I fit some data with high order polynomials as such:
ft = fittype('poly9');
[f,gof] = fit(x,y,ft);
and then turn this into a function:
func = #(x) f.p1*x.^9 + f.p2*x.^8 + f.p3*x.^7 + f.p4*x.^6 + f.p5*x.^5 + f.p6*x.^4 + f.p7*x.^3 + f.p8*x.^2 + f.p9*x + f.p10;
I therefore write out the function by hand. The problem is, this is very prone to typos. Is there a way to create a function automatically from a fit?

You don't need to write the function, you can simply use the object f from the output of fit:
y_model = f(x);
where y_model is the result of input x.

Related

Use a surface fit object from matlab in simulink

I performed a surface fit of some data in Matlab using the fit function.
To be more specific:
func_trim= fit( [hopper_volume, water_ballast_volume], trim, 'poly55');
It returned a multi-variable polynomial:
Linear model Poly55:
func_trim(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3
+ p21*x^2*y + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y
+ p22*x^2*y^2 + p13*x*y^3 + p04*y^4 + p50*x^5 + p41*x^4*y
+ p32*x^3*y^2 + p23*x^2*y^3 + p14*x*y^4 + p05*y^5
Coefficients (with 95% confidence bounds):
p00 = -4.742 (-4.745, -4.74)
p10 = 5.068e-05 (4.969e-05, 5.167e-05)
p01 = 0.001447 (0.001436, 0.001459)
p20 = -3.565e-09 (-3.731e-09, -3.399e-09)
p11 = -6.534e-08 (-6.7e-08, -6.369e-08)
p02 = -1.6e-07 (-1.815e-07, -1.385e-07)
p30 = 5.919e-13 (5.795e-13, 6.043e-13)
p21 = 4.683e-12 (4.552e-12, 4.815e-12)
p12 = 1.129e-11 (9.908e-12, 1.267e-11)
p03 = 1.471e-10 (1.282e-10, 1.659e-10)
p40 = -2.017e-17 (-2.059e-17, -1.975e-17)
p31 = -1.562e-16 (-1.611e-16, -1.513e-16)
p22 = -6.93e-16 (-7.444e-16, -6.417e-16)
p13 = -9.909e-16 (-1.551e-15, -4.308e-16)
p04 = -6.751e-14 (-7.516e-14, -5.986e-14)
p50 = 2.446e-22 (2.392e-22, 2.5e-22)
p41 = 2.186e-21 (2.118e-21, 2.254e-21)
p32 = 1.321e-20 (1.243e-20, 1.4e-20)
p23 = 3.805e-20 (2.969e-20, 4.642e-20)
p14 = 2.262e-20 (-6.978e-20, 1.15e-19)
p05 = 1.059e-17 (9.424e-18, 1.175e-17)
I now want to use this object in the simulink environment.
One option would be to explicitly define this in simulink by just hard-coding it in. However I don't prefer this because the coefficients might change (quite frequently) over time and I would have to repeat this every time.
So in short. Are there any more elegant ways to use this object straight in simulink?
The easiest thing to do is to wrap the fit object into a function handle and then call that from an Interpreted MATLAB function block.
To create the function handle:
>> myFunc = #(u)feval(func_trim,u);
Then with the Interpreted MATLAB function block you'll need to
Mux the x and y signals together, then feed the combined signal into the block.
Use "myFunc(u)" - without the double quotes - as the name of the function to call.
The above is not the fastest approach from an execution pespective, nor will it allow for code generation, but it is by far the easiest/quickest to get something up an running.

Call anonymous function without commas between input variables

I have an anonymous function with 10 variables
now I want to evaluate it with data in a p=1x10 matrix like this:
answer=func(p(1),p(2),p(3),p(4),p(5),p(6),p(7),p(8),p(9),p(10))
i don't want to use this, i need something like:
answer=func(p(:))
but it generates error
can anyone give me a solution?
You seem to have some basic misunderstandings using anonymous functions and its syntax.
For what I think you want to do, you basically have three options:
Option 1
Define the function with 10 input parameters and provide 10 input values - OR expand an input array as comma separated list using {:} which requires an intermediate num2cell step:
func1 = #(a,b,c,d,e,f,g,h,i,j) a + b + c + d + e + f + g + h + i + j
p = num2cell(p)
answer = func1(p{:})
Option 2
Define the function with 1 input parameters using an array with 10 values and provide this array:
func2 = #(p) p(1) + p(2) + p(3) + p(4) + p(5) + p(6) + p(7) + p(8) + p(9) + p(10)
answer = func2(p)
Option 3
The last option to use varargin is really case dependent and could look like:
func3 = #(varargin) [varargin{:}]
p = num2cell(p)
answer = func3(p{:})

Split Anonymous Function into Terms

I am wanting to take a matlab anonymous function of something like #(x) = x + x^2 and split it into a cell array so that
f{1} = #(x) x
f{2} = #(x) x^2
I want to be able to do this with some arbitrary function handle. The best that I have come up with so far is taking in #(x) = x + x^2 as a string and splitting it by the addition signs and appending #(x) to the beginning of this. However, I would like to be able to directly use a function handle as the function argument. It would also be nice as using other variables could lead to some difficulty in the string approach.
I am also considering just taking in a cell array of function handles as an argument, which would be more difficult for the user but easier in my code.
For some background, I'm wanting to do this for some least squares data fitting code that I am writing for a class. This code will be for taking in a model function as an argument and I need to evaluate each term separately for the least squares process. I'm not limiting these models to polynomials and even if a polynomial is the model, I want the option to leave out certain powers in the polynomial. If someone has a better suggestion for taking a model function, that would be great too.
UPDATE: Someone wanted to know what I meant by
I'm not limiting these models to polynomials and even if a polynomial
is the model, I want the option to leave out certain powers in the
polynomial.
For some clarification, I was saying that I didn't want to limit the models to
c0 + c1*x + ... + cn*x^n
in which case I could just take in n as a parameter and create terms from that similar to what happens in polyfit. For example, if I know that my input data fits an even or odd function, I may want one of the following models
c0 + c1*x^2 + c2*x^4 + ... + ck*x^(2k)
c1*x + c2*x^3 + ... + cm*x^(2m-1)
Where k is even and m is odd. Or possibly a model that isn't strictly a polynomial, but keeps the coefficients linear, such as
c0 + exp(x) * ( c1 + c2*x + ... cn*x^(n-1) )
This is an interesting problem. The string should not be split at + signs that are within a parenthesis group. For example, with
f = #(x) x + (x+1)*sqrt(x) + x^2 + exp(x+2);
the string should be split at the first, but not at the second + sign.
This can be accomplished as follows. To detect only + signs that are outside parentheses, add 1 for each opening parenthesis and subtract 1 for each closing parenthesis. Then the desired + signs are those with count 0.
I'm assuming the output should be a cell array of function handles. If it should be a cell array of strings just remove the last line.
F = functions(f);
str = F.function; %// get string from function handle
[pref, body] = regexp(str, '#\(.+?\)', 'match', 'split'); %// pref is the '#(...)' part
body = body{2}; %// body is the main part
ind = cumsum((body=='(')-(body==')'))==0 & body=='+'; %// indices for splitting
body(ind) = '?'; %// '?' will be used as split marker
ff = strcat(pref, strsplit(body, '?')); %// split, and then add prefix
ff = cellfun(#str2func, ff, 'uniformoutput', 0); %// convert strings to functions
Result in this example:
ff{1} =
#(x)x
ff{2} =
#(x)(x+1)*sqrt(x)
ff{3} =
#(x)x^2
ff{4} =
#(x)exp(x+2)

Make sure MATLAB does not recalculate symbolic expression

I am building (my first...) MatLab program, it needs to differentiate an equations symbolically and then use this solution many many times (with different numeric inputs).
I do not want it to recalculate the symbolic differentiation every time it needs to put in a new set of numeric values. This would probably greatly add to the time taken to run this program (which - given its nature, a numeric optimiser, will probably already be hours).
My question is how can I structure my program such that it will not recalculate the symbolic differentiation?
The class in question is:
function [ result ] = GradOmega(numX, numY, numZ, numMu)
syms x y z mu
omega = 0.5*(x^2+y^2+z^2) + (1-mu)/((x+mu)^2+y^2+z^2)^0.5 + mu/((x+mu-1)^2+y^2+z^2)^0.5;
symGradient = gradient(omega);
%//Substitute the given numeric values back into the funtion
result = subs(symGradient, {x,y,z,mu}, {numX, numY, numZ, numMu});
end
I know that I could just symbolically calculate the derivative and then copy-paste it into the code e.g.
gradX = x + ((2*mu + 2*x)*(mu - 1))/(2*((mu + x)^2 + y^2 + z^2)^(3/2)) - (mu*(2*mu + 2*x - 2))/(2*((mu + x - 1)^2 + y^2 + z^2)^(3/2));
gradY = y - (mu*y)/((mu + x - 1)^2 + y^2 + z^2)^(3/2) + (y*(mu - 1))/((mu + x)^2 + y^2 + z^2)^(3/2);
gradZ = z - (mu*z)/((mu + x - 1)^2 + y^2 + z^2)^(3/2) + (z*(mu - 1))/((mu + x)^2 + y^2 + z^2)^(3/2);
But then my code is a bit cryptic, which is a problem in a shared project.
There is a related query here: http://uk.mathworks.com/matlabcentral/answers/53542-oop-how-to-avoid-recalculation-on-dependent-properties-i-hope-a-mathwork-developer-could-give-me-a
But I'm afraid I couldn't follow the code.
Also I am much more familiar with Java and Python, if that helps explain anything.
You could wrap your function into some kind of Function-Factory, which does not return numerical results, but a function that can be evaluated:
(I had to replace the call syms with sym('mu'), because for some reason it kept calling a mutools function in line omega = .... I did also change the call to gradient to make sure the arguments are in correct order, and mu will be treated as constant.)
function GradOmega = GradOmegaFactory()
x = sym('x');
y = sym('y');
z = sym('z');
mu = sym('mu');
omega = 0.5*(x^2+y^2+z^2) + (1-mu)/((x+mu)^2+y^2+z^2)^0.5 + mu/((x+mu-1)^2+y^2+z^2)^0.5;
symGradient = gradient(omega,{'x','y','z'});
GradOmega = matlabFunction(symGradient, 'vars', {'x','y','z','mu'});
end
Then you would call it via:
GradOmega = GradOmegaFactory();
result1 = GradOmega(numX1, numY1, numZ1, numMu1);
result2 = GradOmega(numX2, numY2, numZ2, numMu2);
result3 = GradOmega(numX3, numY3, numZ3, numMu3);
...
Even better:
You could go even more fancy and use a wrapper function GradOmega which builds such a function inside and makes it persistent, to get the same interface you had with your initial approach. The first time you call the function GradOmega the symbolic expression is evaluated, but on each consecutive call you will only have to evaluate the generated function handle, which means it should be nearly as fast as if you hard-coded it.
function result = GradOmega(numX, numY, numZ, numMu)
persistent numericalGradOmega;
if isempty(numericalGradOmega)
numericalGradOmega = GradOmegaFactory();
end
result = numericalGradOmega(numX, numY, numZ, numMu);
end
Use this like you would use your original version
result = GradOmega(numX, numY, numZ, numMu);
Just copy and paste both functions into a single GradOmega.m file. (GradOmega should be the first function in the file.)
Another tip: You can even evaluate this function using vectors. Instead of calling GradOmega(1,2,3,4) and GradOmega(5,6,7,8) afterwards, you can save the time overhead via the call GradOmega([1,5], [2,6], [3,7], [4,8]) using row vectors.
Yet another tip: To clean up your code even more, you could also put the first lines into a separate symOmega.m file.
function omega = symOmega()
x = sym('x');
y = sym('y');
z = sym('z');
mu = sym('mu');
omega = 0.5*(x^2+y^2+z^2) + (1-mu)/((x+mu)^2+y^2+z^2)^0.5 + mu/((x+mu-1)^2+y^2+z^2)^0.5;
This way you don't have to have a copy of this symbolic expression in every file you use it. This can be beneficial if you also want to evaluate Omega itself, as you then can make use of the same Factory-approach listed in this answer. You would end up with the following files: symOmega.m, Omega.m and GradOmega.m, where only the file symOmega.m has the actual mathematical formula and the other two files make use of symOmega.m.

symbolic matlab matrix to latex

Consider the symbolic matlab expression
e = (a_1_1 + a_2_2)*(b_1_1 + b_2_2)
Using latex(e) this yields
\left({{a_{1}}}_{1} + {{a_{2}}}_{2}\right)\, \left({{b_{1}}}_{1} + {{b_{2}}}_{2}\right)
Is it possible to [somehow] use comma as separator between the indices, i.e. to get
\left(a_{1,1} + a_{2,2} \right)\,\left(b_{1,1} + b_{2,2}\right)
I'd be interested in an easy way to do it. An ugly way would be:
eqn = latex(e);
eqn1 = regexprep(eqn,'{{','');
eqn2 = regexprep(eqn1,'}}}_{',',');