fit with lsqcurvefit does not work maybe function maldefined - matlab

I am trying to fit function F to experimental data.
x_tem and yd are both vectors of size (12,1). The function should find the best
fitting value y_tau of the function to the experimental data.
I just can't find the mistake - matlab is showing me the error :
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
The code is:
x_tem=Temp_aero_korrektur(:,1);
yd=Temp_aero_korrektur(:,2);
F = #(y_tau,x_tem)((-1)*((273.15-x_tem)*(273.15-y_tau(1))*8.314* (((17.62*x_tem)/(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53));
yd_tau = lsqcurvefit(F,-40,x_tem,yd);

There are two possibilities here. One is that you do actually want to use matrix operations in your objective function (so that, for example, x_tem/x_tem gives a single scalar value using mrdivide). If this is the case then you should be calling lsqcurvefit with the transpose of x_tem
yd_tau = lsqcurvefit(F,-40,x_tem',yd);
The other option is that you actually meant to calculate your objective function on each value of x_tem (so, for example, using x_tem./x_tem to give a vector the same length as x_tem). If this is the case then your objective function should be
F = #(y_tau,x_tem)((-1)*((273.15-x_tem).*(273.15-y_tau(1)).*8.314.* (((17.62*x_tem)./(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53))
(See documentation for times and rdivide for element-wise operations)

Related

Integrating a function on matlab

I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.

Nonlinear square optimization task in matlab

let us suppose that we have following task:Find the optimal value of weights
so that minimize following equation
where var-means variance of given x1 variable, also we have constraint that sum of these weights should be equal to 1
i have initialized anonymous function and weights for initial points
w=[0.5; 0.5];
>> f=#(x1,x2) (w(1)*w(1)*var(x1)+w(2)*w(2)*var(x2))
f =
#(x1,x2)(w(1)*w(1)*var(x1)+w(2)*w(2)*var(x2))
i think i should use function fmincon,
i have created A matrix
A=[1;1];
and b column
b=[1];
then i tried following fun
weighs=fmincon(f(x1,x2),w,A,b)
but it gives me error
Error using optimfcnchk (line 287)
FUN must be a function, a valid string expression, or an inline function
object.
could you help me please what is wrong? thanks in advance
You need to specify the function in fmincon as a function handle or anonymous function; f(x1,x2) evaluates to a scalar double, not a function handle. fmincon will want to evaluate this function with current values of w to check for the quality of the solution, so it needs a way to feed w as an input.
Thus, you need to
Change the function definition to f(w,x1,x2), i.e.
f=#(w,x1,x2) (w(1)*w(1)*var(x1)+w(2)*w(2)*var(x2))
Write the fmincon call as fmincon(#(u)f(u,x1,x2),...)
However, I would suggest to substitute 1-w(2) for w(1) (or vice versa) in your problem to reformulate it as an unconstrained optimization of one variable (unless w is a real weight, and has to remain between 0 and 1, in which case you still need a constraint).

Converting symbolic equation to fitness function which can be evaluated by genetic algorithm

I have a symbolic equation and wish to convert to a function which can be evaluated by genetic algorithm (ga). I have tried using the matlabFunction and convert the symbolic equation into a matlab file. However, this generated file can only be evaluated by fmincon or patternsearch algorithms and not genetic algorithm. I get this error using ga.
Caused by: Failure in initial user-supplied fitness function evaluation. GA cannot continue.
It seems like the matlabFunction does not generate the format required by ga, can anyone please advise what's the solution/workaround to this problem?
The code are as follow:
N = 24;
X = sym('x',[2*N 1]);
Y = X(1:N);
W = 3.2516e-6.*Y.^3 - 0.0010074.*Y.^2 + 0.390950.*Y+2.2353;
Z = P.*W;
totR = sum(Z);
totR = subs(totR,[P],[price]);
matlabFunction(totR,'vars',{X},'file','objFcn');
% Call to ga
x1 = ga(#objFcn, N*2, A, b, Aeq, beq)
Thanks!
From the documentation for ga:
The fitness function should accept a row vector of length nvars and return a scalar value.
Your objFcn is accepts a column vector and throws an error if a row vector is passed in. You can fix this (I think) by changing this line to:
X = sym('x',[1 2*N]);
If P is non-scalar then you may need to transpose it. Of course, without runnable code there could be all sorts of other things going on too. There are probably other places and ways that you could fix the issue.

Matlab fitting data to a function, where the function is a summation and periodic, syntax

I am attempting to fit temperature data over the last 50 years for the US using the lsqcurvefit, but I am concerned that my fitting function has bad syntax.
The fitting function itself is of the form:
∑(ai+bi*t+ci*t^2)*(cos(vt+p))
Summing from i=1 to N. I would like t to correspond to the time vector called TIME. Ideally the fitting should return values for a, b, c, v, and p which describe the temperature data
I am attempting to fit daily maximum temperatures, a vector called TMAX.
So far I have:
lsqcurvefit(fitFn(1,2,TIME),5,TIME,TMAX)
Where fitFn is defined in a script as
function f = fitFn(i,N,t)
f=0;
for i=1:N
f =#(i,N,a,b,c,v,p,t) f + (a+b*t+c*t^2)*(cos(v*t+p));
end
However whenever I run lsqcurvefit I get the error
Error using
fitFn>#(i,N,a,b,c,v,p,t)f+(a+b*t+c*t^2)*(cos(v*t+p)) (line
4)
Not enough input arguments.
Error in lsqcurvefit (line 199)
initVals.F =
feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function
evaluation. LSQCURVEFIT cannot continue.
I am sure I'm making a simple error but I'm somewhat at a loss. I'm new to MatLab and don't quite understand how to use anonymous functions. Any help would be greatly appreciated.
Thanks for reading
Your fitFn is very wrong. I think this is what you want:
f = #(x,t) (x(1)+x(2)*t+x(3)*t.^2)*cos(x(4)*t+x(5))
lsqcurvefit(f,zeros(1,5),TIME,TMAX)

Calculating derivative and integration using matlab

I am trying to find second derivative of a function, but while initializing my symbols i am getting the following error:
Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'sym'.
The commands I am using are:
syms x a b c L;
u = (a*x(x-L))+(b*x((x^2)-(L^2)))+(c*x((x^3)-(L^3)));
"u" is my function.
I don't know much about MATLAB's symbolic capabilities, but that error is coming from the pieces like
x(x-L)
which MATLAB is interpreting as an indexing operation. Did you mean multiplication there? I.e.
x*(x-L)