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

Antworten (2)

Jan
Jan am 2 Aug. 2011

1 Stimme

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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Sean de Wolski
Sean de Wolski am 2 Aug. 2011

0 Stimmen

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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 2 Aug. 2011

Community Treasure Hunt

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

Start Hunting!

Translated by