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
Jan 2011년 8월 2일

1 개 추천

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
mk612 2011년 8월 2일
ok, I will try again.
Also, should I put . before every mathematical operation ?
for example .* ./
Thanks
Sean de Wolski
Sean de Wolski 2011년 8월 2일
Probably. Unless you want to do a matrix multiplication/division.
mk612
mk612 2011년 8월 3일
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?

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2011년 8월 2일

0 개 추천

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

mk612
mk612 2011년 8월 2일
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