Error: A and B must be floating point scalars - matlab

I am trying to optimize the function using fminsearch and function handle
but, I get the error A and B must be floating point scalars. In detalis,
Error using integral (line 86)
A and B must be floating point scalars.
Error in #(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3))
Error in #(x)((x-f2(x,p)).^2)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in #(p)(integral(#(x)((x-f2(x,p)).^2),-3,3))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
How can I solve this?
i think x-3 become a problem but i cannot deal with it.
x should be variable in f2 in order to do integral with respect to x in q3
Thank you in advance
sigma=0.1;
f2=#(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3));
q3=#(p)(integral(#(x)((x-f2(x,p)).^2),-3,3));
[p, fval] = fminsearch(q3,[0.1 0.4]);

The problem is that when you integrate over x, the function "integrate" gives f2 a vector to evaluate. Which is the same reason you use the dot notation in the function "q3".
The quick fix is to use arrayfun around the "f2" - but you should really consider to adapt to what user20160 suggested in the comments, namely to make full functions, then it will be easier to debug and as it takes in a vector, you can make a for loop, which runs over the inputted endpoints. Standard for-loops are faster than standard arrayfun.

Related

Is it possible to define an integral function and integrate it using `integral`?

I want to do the following integration:
In order to do integration using the function integral in matlab, I define the function
first and then apply integral. Below is my function:
OrderEpsilon2IntegrandIntegralIntegrand=#(T,s)lambda.*(T-s).*exp(-1.*lambda.*s);
OrderEpsilon2IntegrandIntegral=...
#(T,u)integral(#(t)OrderEpsilon2IntegrandIntegralIntegrand(T,t),u,T);
OrderEpsilon2Integrand=#(T,u)...
(T-u).*sigma(u).*v_0-...
OrderEpsilon2IntegrandIntegral(T,u).*exp(lambda.*u).*sigma(u).*v_0;
OrderEpsilon2Integral=...
#(t)arrayfun(#(T)integral(OrderEpsilon2Integrand(T,u),InitialTime,T),t);
However, I have the following error message:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in (T,u)integral(#(t)OrderEpsilon2IntegrandIntegralIntegrand(T,t),u,T)
Error in #(T,u)(T-u).*sigma(u).*v_0-OrderEpsilon2IntegrandIntegral(T,u).*exp(lambda.*u).*sigma(u).*v_0
Error in #(u)OrderEpsilon2Integrand(2.3,u)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
What's the problem to make such error occurs? I have no ideas on the message. Also, I would like to know how to correct the codes. However, there are some constraints on correcting the codes:
First, do not write
explicitly.
Second, do not use integral2. If possible, I would like to define the integrand having the integral.
Why not?:
syms s u T t sg lm
%%sg=u^2+u+1; %%Define sigma expression in here
f=int((T-u)*sg-int(lm*(T-s)*exp(-lm*s),s,u,T)*exp(lm*u),u,t,T)
Resulting in:
T*t + T/lm + (T^2*sg)/2 - t/lm + (sg*t^2)/2 - T^2/2 - 1/lm^2 - t^2/2 + (exp(-T*lm)*exp(lm*t))/lm^2 - T*sg*t

How to do this non linear least square fitting that involves an integral?

I have the following expression:
phi=(a)int(exp(-x(1+(8*pibc)^2))dx),
where the integral is from 0 to inf, and a,b,c are constants.
I know phi, but I do not know the constants a,b and c. That is why I want to do a non linear least square fitting, as I am not sure I will obtain a straight line, in case of obtaining it I would change it.
The thing is that I think I have problems with defining the integral and introducing it in the final expression where I do the non linear least square fitting. Loads of errors appear once I run it.
The code is the following:
%I introduce the data
qy=[0.022 0.023 0.025 0.027 0.028 0.03 0.033 0.037 0.043 0.049 0.058 0.065 0.075 0.094 0.141 0.155 0.192 0.219 0.369];
QY=qy(end);
%I introduce initial guesses for my fitting
initial_guesses=[max(max(qy))/2 QY/10 max(max(qy))/2];
i_g(1,1)=initial_guesses(1,1);
i_g(2,1)=initial_guesses(1,2);
i_g(3,1)=initial_guesses(1,3);
%Let's define my integral
fun=#(x,parameters) exp(-x.*(1+(8*pi()*parameters(2)*parameters(3)).^2));
%We model the fitting
HoffForst_decaydiff=#(parameters) (qy(1:end)-parameters(1)*integral(#(x,parameters)fun,0,Inf));
%options = optimoptions(#lsqnonlin,'Algorithm','levenberg-marquardt');
options = optimoptions(#lsqnonlin,'Algorithm','levenberg-marquardt','TolFun',1e-7,'TolX',1e-7);
%perform NL LS fit
[fit_parameters, resnorm, fit_residuals]=lsqnonlin(HoffForst_decaydiff, i_g, [], [], options);
The errors that appear once I run the last line (lsqnonlin line) are:
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'function_handle'.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 83)
[q,errbnd] = vadapt(#AToInfInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in #(parameters)(qy(1:end)-parameters(1)*integral(#(x,parameters)fun,0,Inf))
Error in lsqnonlin (line 194)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
Can you help me, please?
Cheers!

MATLAB errors when using Lagrange Multipliers?

I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );

Taking Integration Error

I'm new at MATLAB. I want to write code of this equations.
I tried with below code which is written by me.
k=-6:6;
n=-6:6
x= double(k>=0);
h=10*exp(-10*(n-k)).*double((n<=0));
fonk=double(k>=0).*(10*exp(-10*(n-k)).*double(k-n>=0));
for n=1:5
y=integral(#(k)fonk(k,n),6,6);
stem((-6:-6),y);
But It gives
Subscript indices must either be real positive integers or logicals.
Error in untitled2>#(k)fonk(k,n) (line 10)
y=integral(#(k)fonk(k,n),1,6);
Error in integralCalc/iterateScalarValued (line 315)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in untitled2 (line 10)
y=integral(#(k)fonk(k,n),1,6);
errors. I have been googling for this error but I cant find solution. Thanks for Helping.
Modifying your 5th line should be sufficient. You don't need 1~4th lines.
fonk=#(kv,nv) double(kv>=0).*(10*exp(-10*(nv-kv)).*double(kv-nv>=0));
for n=1:5
y(n)=integral(#(z) fonk(z,n),-6,6);
end
stem(y)
You need to define fonk as a function that takes two arguments otherwise integral does not understand it. Also you need to use a vector form of y to store results.

Using fgoalattain() or solve() for Matlab

I am trying to solve a problem (think excel "goal seek") where given a specific rate of return, matlab can solve for the variable "y" (which is used to multiply a matrix).
I've tried using fgoalattain (i don't understand it) and solve
here is the function which basically adds some arrays together to make a cashflow with y being a double. ppam_gen is a n*1 matrix. other_tot is a n*1 matrix.
down_pmt is a double.
function ansirr = cirr(y)
cf_gen_oth = y*ppam_gen + oth_tot
dp = [-down_pmt]
cashflow = [dp;cf_gen_oth]
ansirr = irr(cashflow)
%ansirr = irr([dp; y*ppam_gen + oth_tot])
end
x = fgoalattain(#cirr,0.001,.15,abs(.15))
does not work, the following error is given:
Error using roots (line 28)
Input to ROOTS must not contain NaN or Inf.
Error in irr (line 134)
coeff = roots(fliplr(cf(:,loop)')); % Find roots of polynomial
Error in ppa_model/cirr (line 139)
ansirr = irr(cashflow)
Error in goalcon (line 26)
f = feval(funfcn{3},x,varargin{:});
Error in
fgoalattain>#(y,varargin)feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:})
(line 473)
cfun{3} = #(y,varargin)
feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:});
Error in nlconst (line 746)
[nctmp,nceqtmp] = feval(confcn{3},x,varargin{:});
Error in fgoalattain (line 519)
[xnew,ATTAINFACTOR,LAMBDA,EXITFLAG,OUTPUT]=...
Error in ppa_model (line 152)
r = fgoalattain(#cirr,0.001,.15,abs(.15))
and using symbolic toolbox doesn't work either.
syms y
solve(irr([-down_pmt ; y*ppam_gen + oth_tot]) == 0.15)
throws the error:
Error using assignin
Attempt to add "y" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in ppa_model (line 152)
syms y