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 个)

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 个评论

ok, I will try again.
Also, should I put . before every mathematical operation ?
for example .* ./
Thanks
Probably. Unless you want to do a matrix multiplication/division.
I added . before every mathematical operation.
However now I get another error?
??? Error using ==> lognpdf at 41
Non-scalar arguments must match in size.
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);
pls suggest what I should do?

请先登录,再进行评论。

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)

1 个评论

Hi Sean,
I tried doing this but I am still getting the same error. Could you suggest something else.
Thanks
Mahima

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

提问:

2011-8-2

Community Treasure Hunt

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

Start Hunting!

Translated by