I am trying to solve a double integral where I am solving both inner as well as outer integral with quadgk function.
% The integrand is of course a function of both x and y
integrand = #(x,y) (phi(j,y,X) - phi(j,x,X))*(phi(i,y,X) - phi(i,x,X))/abs(y-x)^(2*s+1)
% The inner integral is a function of x, and integrates over y
inner = #(x) quadgk(#(y)integrand(x,y), x-lambda, x+lambda)
% The inner integral is integrated over x to yield the value of the double integral
dblIntegral = quadgk(inner, -(1+lambda), 1+lambda)
and I am getting this following error:
integrand = #(x,y)(phi(j,y,X)-phi(j,x,X))*(phi(i,y,X)-phi(i,x,X))/abs(y-x)^(2*s+1)
inner = #(x)quadgk(#(y)integrand(x,y),x-lambda,x+lambda)
??? Error using ==> quadgk at 108
A and B must be scalar floats.
Error in ==> #(x)quadgk(#(y)integrand(x,y),x-lambda,x+lambda)
Error in ==> quadgk>evalFun at 344
fx = FUN(x);
Error in ==> quadgk>f1 at 362
[y,too_close] = evalFun(tt);
Error in ==> quadgk>vadapt at 258
[fx,too_close] = f(x);
Error in ==> quadgk at 197
[q,errbnd] = vadapt(#f1,interval);
Error in ==> frational_laplacian at 29
dblIntegral = quadgk(inner, -(1+lambda), 1+lambda)
You're getting that problem because internally quadgk evaluates the passed function using a vector of values. This means that in the line
dblIntegral = quadgk(inner, -(1+lambda), 1+lambda)
the function inner is being called with x being a vector. quadgk requires that a and b (in this case x±lambda) be scalars, so you get the error you've been seeing. If you want to ensure that inner always gets a scalar as input, you can use
dblIntegral = quadgk(#(x)arrayfun(inner,x), -(1+lambda), 1+lambda)
Related
I want to evaluate the double integral of my objective function (named myfunction, see below) using the build-in-function integral2.
I have tried to run this script;
f = #(r,theta) myfunction(r,theta);
integral2(f,0,2,0,2*pi);
where myfunction is the following function:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
los(:,1)=x;
los(:,2)=y;
norm = (sum( sqrt( los(:,1).^2 + los(:,2).^2)));
fkt=norm*r;
end
I am making the integral in polar coordinates, that why fkt=norm*r.
Matlab gives me the following error message:
>> untitled2
Subscripted assignment dimension mismatch.
Error in myfunction (line 8)
los(:,1)=x;
I can't figure out, what the problem is.
There are two things that can be improved:
los is undefined
los(:,1) is a column while x is a row, so the assignment has to fail.
You can correct this by defining los and change your assignment. For instance:
los = NaN(numel(r), 2);
los(:,1) = x';
los(:,2) = y';
But, why do you need the variable los? Just remove it and the error will be gone:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
norm = (sum( sqrt(x.^2 + y.^2)));
fkt=norm*r;
end
Best,
x is a matrix and you try to assign it to a column vector.
I am facing an error when using the function fminunc in the context of a maximum-likelihood estimation. I am afraid that it is very straight forward however my experience with MATLAB is very limited.
The function "normal" contains the log-likelihood function. I seek to estimate the expectation and std. deviation of a normal distribution given the observations stored in the variable x.
function f = normal(X, theta)
mean = theta(1);
sigma = theta(2);
z = (X-mean)./sigma;
f = -(sum(-log(sigma) -(1/2).*z.^2 ));
I basically execute the following code:
theta = [1,1]
f = #(theta)normal(x, theta)
[est, fval, exitflag, output, grad, hessian] = fminunc('normal', x, theta)
The error is the following:
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release
Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.
In createOptionFeedback at 34
In prepareOptionsForSolver at 31
In fminunc at 157
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release
Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.
In fminunc at 203
Error using feval
Undefined function 'normal' for input arguments of type 'double'.
Error in fminunc (line 254)
f = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
Unfortunately the manual did not help me to fix the code. Calling
[est, fval, exitflag, output, grad, hessian] = fminunc(f, x, theta)
did not help either. What am I doing wrong?
Thank you in advance!
You have called fminunc with the wrong sintax, please refer to the documentation.
A way to fix your code is by defining the function normal to accept only one parameter: theta.
function f = normal(theta)
global X
mean = theta(1);
sigma = theta(2);
z = (X-mean)./sigma;
f = -(sum(-log(sigma) -(1/2).*z.^2 ));
and call fminunc with
global X
X = randn(100, 1); % A possible time series.
theta0 = [1,1];
[est, fval, exitflag, output, grad, hessian] = fminunc(#normal, theta0);
need to find a set of optimal parameters P of the system y = P(1)*exp(-P(2)*x) - P(3)*x where x and y are experimental values. I defined my function
f = #(P) P(1)*exp(-P(2)*x) - P(3)*x
and
guess = [1, 1, 1]
and tried
P = fminsearch(f,guess)
according to Help. I get an error
Subscripted assignment dimension mismatch.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
I don't quite understand where my y values would fall in, as well as where the function takes P from. I unfortunately have no access to nlinfit or optimization toolboxes.
You should try the matlab function lsqnonlin(#testfun,[1;1;1])
But first make a function and save in an m-file that includes all the data points, lets say your y is A and x is x like here below:
function F = testfun(P)
A = [1;2;3;7;30;100];
x = [1;2;3;4;5;6];
F = A-P(1)*exp(-P(2)*x) - P(3)*x;
This minimized the 2-norm end gives you the best parameters.
I have a huge function to integrate:
syms x y
f=(228155022448185.*(cos((2.*pi).*y)./exp(131738205584307./(35184372088832*x)) - 1)*(cos((8.*pi).*y)/exp(131738205584307./(8796093022208*x)) - 1)*(cos((8.*pi).*y)/exp(131738205584307./(8796093022208.*x)) + cos((18.*pi).*y)/exp(1185643850258763./(35184372088832.*x)) - 2))/((18014398509481984. *(x.^2)).*exp(x. * ((1981232555272083.*(y.^2))/2251799813685248 - y./16 + 1./16)))
I need to integrate it (x:[0,inf) and y:[0,1]), but I receive an error for quad2d and dblquad.
quad2d(quadfun,0,100,0,1)
??? Error using ==> quad2d>tensor at 350
Integrand output size does not match the input size.
Error in ==> quad2d at 164
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
and
dblquad(quadfun,0,100,0,1)
??? Error using ==> dblquad>innerintegral at 74
Inputs must be floats, namely single or double.
Error in ==> quad at 76
y = f(x, varargin{:});
Error in ==> dblquad at 53
Q = quadf(#innerintegral, ymin, ymax, tol, trace, intfcn, ...
Could you explain why these errors appear? And how can I fix it?
The quad function family doesn't work with symbolic math. Instead, you can either:
Use symbolic integration with int. To compute a double integral, invoke int twice consecutively, each time with a different integration variable.
Define an equivalent regular function that accepts non-symbolic parameters and pass its handle to quad. I'd do it with an anonymous function -- just start your definition with f = #(x, y) instead of f =, and that's it (also remember that f is a function handle now, so you don't need to write the ampersat (#) when passing it around).
Trying to compute the following expression,
quad(#(n)quad(#(m)unifpdf(m-n,0,1),-10,10),-10,10)
But I get a message saying:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> #(m)unifpdf(m-n,0,1)
Can you please let me know how to fix this?
The problem here is the following requirement:
The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.
unipdf seems to satisfy this requirement, but quad itself is not.
To fix this problem you need to write a wrapper function that accepts a vector argument x, evaluates inner integral using quad function, and return a vector of results:
function [r] = Test()
r = quad(#(n)InnerIntegral(n),-10,10);
end
function [y] = InnerIntegral(n)
y = zeros(size(n));
for i = 1 : length(n)
y(i) = quad(#(m)unifpdf(m - n(i), 0, 1), -10, 10);
end;
end