Error In Fminsearch At
Support Answers MathWorks Search MathWorks.com MathWorks Answers Support MATLAB Answers™ MATLAB Central Community Home MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Ask Answer Browse More Contributors Recent Activity Flagged Content Flagged as Spam Help MATLAB Central Community Home MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Ask Answer Browse More Contributors Recent Activity Flagged Content Flagged as Spam Help Trial software Tazmusica (view profile) 2 questions 0 answers 0 accepted answers Reputation: 0 Vote0 Function - Problem with fminsearch - subscripted assignment dimension mismatch Asked by Tazmusica Tazmusica (view profile) 2 questions 0 answers 0 accepted answers Reputation: 0 on 19 May 2011 Accepted Answer by John D'Errico John D'Errico (view profile) 4 questions 1,860 answers 677 accepted answers Reputation: 4,288 116 views (last 30 days) 116 views (last 30 days) I am writing a function that is part of a larger project as a demonstration for some students. The program is supposed to do a least squares fit on functions inputted.The error is as follows:??? Subscripted assignment dimension mismatch.Error in ==> fminsearch at 191 fv(:,1) = funfcn(x,varargin{:});The program is: function [c,res,model1]=lstsq(funct,xdata,ydata,start_point) model1=@fun1; options=optimset('Display','iter','MaxIter',1e40,'MaxFunEvals',1e40,'TolX',1e-40,'TolFun',1e-40); [c,res]=fminsearch(model1,start_point,options); function [res,Funct]=fun1(c) Funct=str2func(funct); res=Funct(c,xdata)-ydata; end endYes, I realize that there is a nonlinear least squares program already, but I reiterate that this is for a demonstration, so I need to understand why this program gives the error it does. Here are the inputs:>>lstsq('@(c,xdata)(10.^(-xdata)./(10.^(-xdata)+c(1)))',X,Y,1)X=[1.20000000000000;2;3.06000000000000;3.77000000000000;4.03000000000000;4.42000000000000;4.90000000000000;8.14000000000000;8.84000000000000;8.88000000000000;10.0800000000000;11.3600000000000](this is a 12x1 matrix)Y=1;0.975446654000000;0.865950488000000;0.585195335000000;0.418113;25000000;0.236419836000000;0.0907522820000000;0;0;0;0;0](this is a 12x1 matrix)Any thoughts as to why fminsearch r
Support Support Newsreader MathWorks Search MathWorks.com MathWorks Newsreader Support MATLAB Newsgroup MATLAB Central Community Home MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Post A New Message Advanced Search Help MATLAB Central Community Home MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Post A New Message Advanced Search Help Trial software fminsearch: Subscripted assignment dimension mismatch. Subject: fminsearch: Subscripted assignment dimension mismatch. From: https://www.mathworks.com/matlabcentral/answers/7756-function-problem-with-fminsearch-subscripted-assignment-dimension-mismatch Moh Moh (view profile) 1 post Date: 20 Feb, 2012 03:30:13 Message: 1 of 6 Reply to this message Add author to My Watch List View original format Flag as spam Hi everyone, In running the following code, Matlab sends me "Subscripted assignment dimension mismatch Error in ==> fminsearch at 205 fv(:,1) = https://www.mathworks.com/matlabcentral/newsreader/view_thread/317082 funfcn(x,varargin{:});" Went over the code several times, yet cannot see what is wrong. Can someone help me out please: clear; N = 1000; rand('seed', 20); income = unifrnd(0,1,[N,1]); income_s = sort(income); t_star = .3; z_bar = 0.4; % set arbitraty z alpha = .2; rho = -100; for i= 500 % I need the loop for some reasons y_i = income_s(i); c = [y_i N rho alpha t_star]; z_star = fminsearch(@(z) u_fn_i(z, c), z_bar); end ============== and here is u_fn_i function ============== function u_fn = u_fn_i(z, c) global income_s; y_i = c(1); N = c(2); rho = c(3); alpha = c(4); t_star = c(5); inv_rho = 1/rho; y_z = quantile(income_s, z); y_star = mean(income_s(income>y_z)); ind = find(income_s here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://stackoverflow.com/questions/13435836/error-while-using-fminsearch workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs http://stackoverflow.com/questions/35195060/error-in-fminsearch-line-191-fv-1-funfcnx-varargin Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join error in them; it only takes a minute: Sign up Error while using fminsearch up vote 1 down vote favorite 1 I am using fminsearch to minimize the error between the covariance at coarse scale and the average of covariance at fine scale by perturbing certain parameters. These are 2 lines of code lines using fminsearch in which I am calling error in fminsearch the objective function minimize_me with three arguments which I intend to perturb: opts = optimset('display', 'iter'); [x,fval,exitflag] = fminsearch( @(x) minimize_me(x(1), x(2), x(3)), [2, 5, 90], opts); The function minimize_me is shown following and it uses couple of more functions inside its body: function diff = minimize_me(a_minor, a_major, theta) %# Grid and model parameters nModel=50; nModel_want=1; nI_grid1=5; Nth=1; nRow.Scale1=5; nCol.Scale1=5; nRow.Scale2=5^2; nCol.Scale2=5^2; nCell.Scale1=nRow.Scale1*nCol.Scale1; %% Covariance computation, averaging and difference of coarse and fine scale averaged covariances % Reading files by using the function 'general_gslib_file_to_mat.mat' [Deff_matrix_NthModel,~,~]=general_gslib_file_to_mat(nModel,nCell.Scale1,nModel_want,nI_grid1,Nth,'effective_dispersivity_coarsegrid5x5_gslib_format'); %# Maximum value of covariance/variogram at coarse scale sill = var(reshape(Deff_matrix_NthModel,nCell.Scale1,1)); % variance of the coarse data matrix of size (nRow.Scale1 X nCol.Scale1) %% Compute the covariance at different lags using the function general_CovModel.m for ihRow = 1:nRow.Scale1 for ihCol = 1:nCol.Scale1 [cov.Scale1(ihRow,ihCol),heff.Scale1(ihRow,ihCol)] = general_CovModel(theta, ihCol, ihRow, a_minor, a_major, sill, 'Exp'); end end for ihRow = 1:nRow.Scale2 for ihCol = 1:nCol.Scale2 [cov.Scale2(ihRow,ihCol),heff.Scale2(ihRow,ihCol)] = general_CovModel(theta, ihCol/(nCol.Scale2/nCol.Scale1), ihRow/(nRow.Scale2/nRow.Scale1), a_minor, a_major,... sill/(nRow.Scale2*nCol.Scale2), 'Exp'); end end %# Scale-up of fine scale values by averaging which is done using the function general_AverageProperty.m [covAvg.Scale2,var_covAvg.Scale2,varNorm_covAvg.Scale2] = general_AverageProperty(nRow.Scale2/nRow.Scale1,nCol.Scale2/nCol.Scale1,1,nRow.Scale1,nCol.Scale1,1,cov.Scale2,1); %# here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error in fminsearch (line 191) fv(:,1) = funfcn(x,varargin{:}); up vote 0 down vote favorite 1 I want to minimize the below function which uses few constants(Hz,h,LR,k,T), but ending up with an error,....Please get me out of this.... I have loaded .rpt file which contains values as shown on top of code.......Thanks in advance. 3.25E+008 9.55E+002 3.03E+008 1.61E+005 2.77E+008 3.35E+005 2.54E+008 1.98E+006 1.17E+008 1.48E+003 1.13E+008 6.20E+004 1.00E+008 7.96E+005 9.15E+007 2.12E+003 format compact format long Hz = 3; h = 3.99E-10; LR = 0.1; k = 8.3145; T = 297.15; load sdata_ndata.rpt count = size(sdata_ndata,1); xdata = zeros(count,1);% Stress ydata = zeros(count,1);% Nf for i=1:count xdata(i) = sdata_ndata(i,1); ydata(i) = sdata_ndata(i,2); end %Function to calculate the sum of residuals for a given p1 and p2 fun = @(p) sum((ydata-(((p(1)*(Hz*h*(1-LR))*xdata)*(exp((p(2))/(k*T))))/(((k*T)^2)*((exp((p(1)*xdata)/(k*T)))-(exp((p(1)*LR*xdata)/(k*T))))))).^2); %starting guess pguess = [1.338463e-003;1.234006e+005]; %optimise [p,fminres] = fminsearch(fun,pguess) matlab fminsearch share|improve this question edited Feb 4 at 7:39 asked Feb 4 at 7:33 vivek 35 looks like a problem in your fun function... but I can't reproduce your mistake because of don't know what is xdata, ydata (can't load sdata_ndata.rpt). Can you show all necessary data for calculation? –Mikhail_Sam Feb 4 at 7:49 Your need to replace the matrix division ...(k*T))))/(((k*T)^2)... by an element-wise division ...(k*T))))./(((k*T)^2).... –Nemesis Feb 4 at 8:12 Thanks Mikhail.......The sdata_ndata.rpt file contains the data shown at the begining of the code. (sdata is in column of 3.25E+008. and ndata is in column of 9.55E+002 ) –vivek Feb 4 at 8:12 BTW: You can write xdata = sdata_ndata