Prev: xlsread_mod
Next: Reading .txt file and calculating GPA
From: diana van dijk on 23 Mar 2010 17:18 After having read more carefully about using interp2/interpn (ZI=interpn(X,Y,Z,XI,ZI), I saw that I hadn't transformed the X and Y vectors to ndgrid (or meshgrid) matrices. Let's say the new code is: xx=linspace(0,5,5); yy=linspace(0,2,4); [X Y] = ndgrid(xx,yy); Z=zeros(size(X)); beta = linspace(0,4,40); gamma=lognpdf( beta,m,s ); for i = 1:length(X) for j = 1:length(Y) x = X(i); y = Y(j); [Out ZZ] = fmincon( @(In) namefile(In,x,y,Z),[2 1],[],[],[],[],[0 -Inf],[x Inf]); Sstar(i,j) = Out(1); Gstar(i,j) = Out(2); ZI(i,j) = -ZZ; end Z = ZI; end The corresponding Function File is: function [answer2] = namefile(In,x,y,Z) s = In(1); g = In(2); answer1 = par1*s - par2*s^2 - par3*s/(par4*x) - par5*g; XI = beta*(x - s)^alpha; YI = (1-gamma)*y + g; ZI = interpn( X,Y,Z,XI,YI,'cubic'); answer2 = -( answer1 + gamma * ZI'); %with the idea that ZI is a vector and needs to be transposed to be multiplied with vector gamma When I run the code I get the error: FMINCON cannot continue because user supplied objective function failed with the following error: Error using ==> mtimes Inner matrix dimensions must agree. Error in ==>[Out ZZ] = fmincon( @(In) namefile(In,x,y,Z),[2 1],[],[],[],[],[0 -Inf],[x Inf]); I'm thinking it's because XI is a vector (with length(beta)) and YI is a scalar value. As I would like ZI to be a vector, a solution is to transform scalar YI into a vector of the same length as XI, where all elements in the vector are 'ones', except for the location over which the loop takes place. On that location the value of YI will appear. I thought one way is to code this like: XI = 0:1:2; YI = 2:1:5; M = diag(YI - 1) + 1; for i=1:length(YI) M(i,:) end ans = 2 1 1 1 ans = 1 3 1 1 ans = 1 1 4 1 ans = 1 1 1 5 In the function file I adjusted it as follows: XI = beta*(x - s)^alpha; YI_old = (1-gamma)*y + g; YI_new = diag(YI_old - 1) + 1; for i=1:length(YI_old) YI_new(i,:) end ZI = interpn( X,Y,Z,XI,YI_new(i,:),'cubic'); answer2 = -( answer1 + gamma * ZI'); But, I get the same error message: FMINCON cannot continue because user supplied objective function failed with the following error: Error using ==> mtimes Inner matrix dimensions must agree. Then I also tried in the function file to repeat the scalar value YI over length(XI) to get a vector: XI = beta*(x - s)^alpha; YI_old = (1-gamma)*y + g; YI_new = repmat( YI_old, size(XI) ); ZI = interpn( X,Y,Z,XI,YI_new,'cubic'); answer2 = -( answer1 + gamma * ZI'); This gives me the following error: FMINCON cannot continue because user supplied objective function failed with the following error: Error using ==> interpn at 160 Arrays X1,X2,X3, etc. must be the same size as Z. I'm out of options. I would really appreciate some further help. Thanks!
From: Hernan Romero on 25 Mar 2010 10:03 Diana, I think there is a dimension mismatch inside the function namefile. Try taking fmincon out of the loop in order to debug. In other words, try running this code: for i = 1:length(X) for j = 1:length(Y) x = X(i); y = Y(j); namefile(In,x,y,Z); end end Put a break point in namefile and go from there. I hope this helps, -Hernan "diana van dijk" <matlab_di(a)yahoo.com> wrote in message news:hobb6b$a81$1(a)fred.mathworks.com... > After having read more carefully about using interp2/interpn > (ZI=interpn(X,Y,Z,XI,ZI), I saw that I hadn't transformed the X and Y > vectors to ndgrid (or meshgrid) matrices. > > Let's say the new code is: > > xx=linspace(0,5,5); > yy=linspace(0,2,4); > [X Y] = ndgrid(xx,yy); > Z=zeros(size(X)); > > beta = linspace(0,4,40); > gamma=lognpdf( beta,m,s ); > > for i = 1:length(X) > for j = 1:length(Y) > x = X(i); > y = Y(j); > [Out ZZ] = fmincon( @(In) namefile(In,x,y,Z),[2 > ],[],[],[],[],[0 -Inf],[x Inf]); > Sstar(i,j) = Out(1); > Gstar(i,j) = Out(2); > ZI(i,j) = -ZZ; > end > Z = ZI; > end > > The corresponding Function File is: > > function [answer2] = namefile(In,x,y,Z) > s = In(1); g = In(2); > answer1 = par1*s - par2*s^2 - par3*s/(par4*x) - par5*g; > XI = beta*(x - s)^alpha; YI = (1-gamma)*y + g; ZI = interpn( > X,Y,Z,XI,YI,'cubic'); > answer2 = -( answer1 + gamma * ZI'); %with the idea that ZI is a vector > and needs to be transposed to be multiplied with vector gamma > > When I run the code I get the error: > FMINCON cannot continue because user supplied objective function failed > with the following error: > Error using ==> mtimes > Inner matrix dimensions must agree. > Error in ==>[Out ZZ] = fmincon( @(In) namefile(In,x,y,Z),[2 > 1],[],[],[],[],[0 -Inf],[x Inf]); > > I'm thinking it's because XI is a vector (with length(beta)) and YI is a > scalar value. As I would like ZI to be a vector, a solution is to > transform scalar YI into a vector of the same length as XI, where all > elements in the vector are 'ones', except for the location over which the > loop takes place. On that location the value of YI will appear. > I thought one way is to code this like: > > XI = 0:1:2; > YI = 2:1:5; > M = diag(YI - 1) + 1; > for i=1:length(YI) > M(i,:) > end > > ans = 2 1 1 1 > ans = 1 3 1 1 > ans = 1 1 4 1 > ans = 1 1 1 5 > > In the function file I adjusted it as follows: > > XI = beta*(x - s)^alpha; YI_old = (1-gamma)*y + g; YI_new = diag(YI_old - > 1) + 1; for i=1:length(YI_old) > YI_new(i,:) > end > ZI = interpn( X,Y,Z,XI,YI_new(i,:),'cubic'); > answer2 = -( answer1 + gamma * ZI'); > But, I get the same error message: > FMINCON cannot continue because user supplied objective function failed > with the following error: > Error using ==> mtimes > Inner matrix dimensions must agree. > > Then I also tried in the function file to repeat the scalar value YI over > length(XI) to get a vector: > XI = beta*(x - s)^alpha; YI_old = (1-gamma)*y + g; YI_new = repmat( > YI_old, size(XI) ); > ZI = interpn( X,Y,Z,XI,YI_new,'cubic'); > answer2 = -( answer1 + gamma * ZI'); > > This gives me the following error: > FMINCON cannot continue because user supplied objective function failed > with the following error: > Error using ==> interpn at 160 > Arrays X1,X2,X3, etc. must be the same size as Z. > > I'm out of options. I would really appreciate some further help. Thanks! >
|
Pages: 1 Prev: xlsread_mod Next: Reading .txt file and calculating GPA |