integrating in a for loop
显示 更早的评论
Dear All,
I have written a for loop to solve two simultaneous equations (using fsolve function)to give me x(1) and x(2). Using these values , I compute various functions of these and of calibrated parameters.
One such function requires me to integrate a function of x(1) involing log normal pdf and cdf. I tried using the trapz and quad command but it always gave me errors.
I coded as gridzi=linspace(x(1)*0.0001,zfstar-0.0001,1000);
zicurl=trapz(gridzi,((gridzi.^(teata-1)).*(lognpdf(gridzi,mue,sd)./(1-(logncdf(x(1),mue,sd))))));% i need to find this integral
but it always gave an error
??? Error using ==> mpower Inputs must be a scalar and a square matrix.
Error in ==> model1lognormal at 27 zicurl=trapz(gridzi,((gridzi^(teata-1))*(lognpdf(gridzi,mue,sd)/(1-(logncdf(x(1),mue,sd)))))); % i need to find this integral
Error in ==> fsolve at 254 fuser = feval(funfcn{3},x,varargin{:});
Error in ==> runmodel1lognormalplot at 56 [x,fval,exitflag,output]=fsolve(@model1lognormal,x0plus,options,tariff);
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Please suggest how I should solve this. I urgently need to do this for my thesisi
回答(2 个)
Jan
2011-8-2
The code you have posted differs from the one shown in the error message:
The uses ".^" with dot:
zicurl=trapz(gridzi,((gridzi.^(teata-1)).*(lognpdf(gridzi,mue,sd)./(1-(logncdf(x(1),mue,sd))))));
But in the error message the operator is "^":
Error in ==> model1lognormal at 27 zicurl=trapz(gridzi,((gridzi^(teata-1)) * (lognpdf(gridzi,mue,sd) / (1-(logncdf(x(1),mue,sd))))));
Perhaps you've modified the function but did not save it?
3 个评论
mk612
2011-8-2
Sean de Wolski
2011-8-2
Probably. Unless you want to do a matrix multiplication/division.
mk612
2011-8-3
Sean de Wolski
2011-8-2
You probably want to do an element-by-element power (x.^blah). You're trying to do a matrix power, hence the error in mpower.
The solution is simple, add a '.' in front of every '^'.
note the difference between
1.^ones(2)
and
1^ones(2)
类别
在 帮助中心 和 File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!