integrating in a for loop
Mostrar comentarios más antiguos
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
Respuestas (2)
Jan
el 2 de Ag. de 2011
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 comentarios
mk612
el 2 de Ag. de 2011
Sean de Wolski
el 2 de Ag. de 2011
Probably. Unless you want to do a matrix multiplication/division.
mk612
el 3 de Ag. de 2011
Sean de Wolski
el 2 de Ag. de 2011
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 comentario
mk612
el 2 de Ag. de 2011
Categorías
Más información sobre Numerical Integration and Differentiation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!