Getting Error using quad2d

7 views (last 30 days)
Kam Selley
Kam Selley on 11 Apr 2013
Hello,
Below if my function that I am attempting to use for an optimization problem but I keep getting the error that my input arguments do not match my output. I know the error is coming from these lines:
funTRmvn = @(x1,x2)((1/(((2*pi)^(P/2)).*((det([vsqr(1) v12;v21 vsqr(2)])).^(1/2))))*L);
TRmvn = quad2d(funTRmvn,a1,b1,a2,b2);
But I still am unsure what I am supposed to do to fix this. Could someone help me please?
Kam
function [history,searchdir] = JSM
% Set up shared variables with OUTFUN history.x = []; history.fval = []; searchdir = [];
% call optimization x0 = [3,2,4,8,10,6,4,5]; %goal = [-.04]; %weight = abs(goal); options = optimset('outputfcn',@outfun,'display','iter',... 'Algorithm','active-set'); %[x,fval] = fgoalattain(@MaxFunctionProblem,x0,goal,weight,[],[],[],[],[],[],@confun,options); [x,fval] = fmincon(@MinFunctionProblem,x0,[],[],[],[],[],[],@confun,options);
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
text(x(1)+.15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
display(fval) display(x)
% Uunder0 = [u(1);u(2)]; % Sigma0 = [vsqr(1) v12;v21 vsqr(2)]; % xUnder = [x1;x2]; % % Uunder0 = [x(1);x(2)]; % Sigma0 = [x(3) x(4);x(5) x(6)]; u(1) = x(1); u(2) = x(2); vsqr(1) = x(3); vsqr(2) = x(4); x1 = x(5); x2 = x(6); %xUnder = [x(7);x(8)]; v12 = x(7); v21 = x(8);
%data=round(data*100)/100 %str2num(sprintf('%5.2f',stop))
function ELoss = MinFunctionProblem(x)
% Uunder0 = [u(1);u(2)]; % Sigma0 = [vsqr(1) v12;v21 vsqr(2)]; % xUnder = [x1;x2];
%Uunder0 = [x(1);x(2)]; %Sigma0 = [x(3) x(4);x(5) x(6)]; u(1) = x(1); u(2) = x(2); vsqr(1) = x(3); vsqr(2) = x(4); x1 = x(5); x2 = x(6); %xUnder = [x(7);x(8)]; v12 = x(7); v21 = x(8);
P = 2;
% Process Means %u(1) = 2; %u(2) = 4; % u3 = ;
% Estimated Means % xbar1 = ; % xbar2 = ; % xbar3 = ;
% Target Values t(1) = 2; t(2) = 4; % t3 = ;
% Unit Cost to shift Process Mean c1(1,1) = 10; c1(1,2) = 16; % c13 = ;
% Specifications USL1 = 3; LSL1 = 1; USL2 = 6; LSL2 = 2; % USL3 = ; % LSL3 = ;
T1 = 2;%(USL1 - LSL1); T2 = 4;%(USL2 - LSL2); % T3 = (USL3 - LSL3);
% Half-width of Specs A(1) = T1/2; A(2) = T2/2; %A(3) = T3/2;
% Process Variances % variance(1) = 4; % variance(2) = 8; % v3 = ;
% Unit Costs associated with Variances c2(1) = 40; c2(2) = 100; % c23 = ;
% Unit Costs assoc. with covariances c3(1,2) = 50;%c312
% Unit Costs of non-conformance for characteristic i % c4(1) = 80; % c4(2) = 160;
% Constraints on Process Means % alpha1 = .05; % alpha2 = .1; % % alpha3 = .05; % % ProcMeanConstraint1 =(u1-t1)/A1; % ProcMeanConstraint2 =(u2-t2)/A2; % % ProcMeanConstraint3 =(u3-t3)/A3; % % % Constraints on Process Variance % v1min = 2; % v2min = 3; % % v3min = ; % % v1max = 8; % v2max = 12; % v3max = ;
a1 = 0.5; a2 = 1.5; b1 = 6; b2 = 12;
c4 = 40;
% Minimization Function
%Finding MVN % Uunder0 = [u(1);u(2)]; % Sigma0 = [vsqr(1) v12;v21 vsqr(2)]; % xUnder = [x1;x2];
%function 1 Sum1 = 0; for i = 1:P fun1 = (c1(i))*(((u(i)-t(i))/A(i))^2 + (vsqr(i)^2)); Sum1 = Sum1+fun1; end ELf1 = Sum1;
%function 2 Sum2 = 0; for i = 1:P fun2 = (c2(i))*((vsqr(i)^2)/A(i)^2); Sum2 = Sum2+fun2; end ELf2 = Sum2;
%function 3 Sum3 = 0; for i = 1:P for j = i+1:P
fun3 = (c3(i,j))*(((u(i)-t(i))*(u(j)-t(j)))/(A(i)*A(j)));
Sum3 = Sum3+fun3;
end
end
ELf3 = Sum3;
L = exp(-(((transpose([x1;x2]-[u(1);u(2)])))*(inv([vsqr(1) v12;v21 vsqr(2)]))*([x1;x2]-[u(1);u(2)]))); % size(Uunder0) % size(x1) a1 = 2; a2 = 3; b1 = 1; b2 = 4;
%function 4 % Fmvn = mvnrnd(Uunder0,Sigma0); % Fmvn = (1/(((2*pi)^(P/2))*((det(Sigma0))^(1/2))))*(exp((transpose(-(x1-Uunder0)))*(inv(Sigma0))*(x2-Uunder0))); funTRmvn = @(x1,x2)((1/(((2*pi)^(P/2)).*((det([vsqr(1) v12;v21 vsqr(2)])).^(1/2))))*L); nargin(funTRmvn) TRmvn = quad2d(funTRmvn,a1,b1,a2,b2); % funPHIc = @(x1,x2) Fmvn/TRmvn; % PHIc = integral2(funPHIc,LSL1,USL1,LSL2,USL2); % PHInc = 1-PHIc; % fun4 = (c4)*PHInc; %((var(i)^2)/A(i)^2); % ELf4 = fun4;
ELoss = ELf1 + ELf2 + ELf3 + ELf4;
end
%display % disp (['Theta0 = ',num2str(Theta0)]) % disp (['Theta0 = ',num2str(Theta1)]) % disp (['Theta0 = ',num2str(Theta2)]) % disp (['Theta0 = ',num2str(Theta3)])
function [c, ceq] = confun(x)
%Uunder0 = [x(1);x(2)]; %Sigma0 = [x(3) x(4);x(5) x(6)]; u(1) = x(1); u(2) = x(2); vsqr(1) = x(3); vsqr(2) = x(4); x1 = x(5); x2 = x(6); %xUnder = [x(7);x(8)]; v12 = x(7); v21 = x(8);
a1 = 0.5; a2 = 1.5; b1 = 6; b2 = 12;
c = [a1-x1 x1-b1 a2-x2 x2-b2]; ceq = []; end end

Answers (1)

Mike Hosea
Mike Hosea on 12 Apr 2013
Edited: Mike Hosea on 12 Apr 2013
Let f be the integrand function and let Z = f(X,Y). QUAD2D requires that
  1. size(Z) matches size(X) and size(Y). The inputs X and Y will be matrices.
  2. The output matrix Z has the property that Z(i,j) = f(X(i,j),Y(i,j)) for all i and j.
Test your function f on a small rectangular matrix to make sure that it works like this. If you can only code f so that accepts scalars, instead of passing f directly, pass in
ff = @(x,y)arrayfun(f,x,y)
(This is for the case where f is defined as an anonymous function. If f is defined as the MATLAB file f.m, put '@' in front of the f, i.e. "arrayfun(@f,...")).
However, you are not ready for that because this statement
funTRmvn = @(x1,x2)((1/(((2*pi)^(P/2)).*((det([vsqr(1) v12;v21 vsqr(2)])).^(1/2))))*L);
makes funTRmvn a constant function because x1 and x2 do not appear here. I see you have used x1 and x2 in the definition of the constant L, but MATLAB is not a symbolic environment. You can make L a function of x1 and x2 via
L = @(x1,x2) exp(-(((transpose([x ...
and then use "L(x1,x2)" instead of just L in funTRmvn, but I'm not sure this is quite what you were going for. Bottom line is that you need to sort out the definition of the integrand first. When you have that integrand function working properly, then move on to using QUAD2D. Build it from the ground up. You only proceed to the next higher level activity when the lower level seems to be working properly.
  2 Comments
Kam Selley
Kam Selley on 13 Apr 2013
Yes that makes so much sense now. I did place "L(x1,x2)" into the function but now I am getting the error "Error using -" "Matrices must agree". It was doing that before which is why I made the L function, but I cannot see why it is saying that. Below is what I made the function:
funTRmvn = @(x1,x2)((1/(((2*pi)^(P/2)).*((det([vsqr(1) v12;v21 vsqr(2)])).^(1/2)))).*(L(x1,x2)));
Any help?
Mike Hosea
Mike Hosea on 15 Apr 2013
The text of your problem above still contains the line "L = exp(..." instead of "L = @(x1,x2)exp(...", and it is still formatted in a way that is very difficult, if not impossible, to interpret. Please edit the question to reflect what you are trying now. It is critical that all code be formatted using the "{} Code" button, not just some lines, because without the carriage returns I can't tell for sure what's supposed to be commented or not. It would also be helpful just to make it as compact as you can by removing all commented code. We're just working on syntax at this point. I want to be able to copy/paste your code and see what you are seeing.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!