error when putting a matrix in the exp function- please review code and help me resolve! the error reads: ??? Subscript indices must either be real positive integers or logicals.

The error occurs at line 105, the variable assigned is cd. Thank you so much!!
%%Calista
%Spring 2011
%CIVE 203
close all; clear all; clc;
%Empirical CDF of Earthquake data
mag = xlsread('Japan_Quake0.xls','Sheet1','K2:K212');
x = linspace(0,9.9,10000);
y = mag(~isnan(mag));
%specify levels of risk
%magnitudes exceeded < 50%,25%,10%,5%,and 1% of time respectively:
levels = [0.5,0.75,0.9,0.95,0.99];
figure(1)
cdfplot(mag);
hold on
%Fit to Normal Distribution
[muhat,sigmahat] = normfit(y);
norm = normcdf(x,muhat,sigmahat);
norm2 = norminv(levels,muhat,sigmahat);
%Fit to Lognormal Distribution
parmhat = lognfit(y);
log = logncdf(x,parmhat(1),parmhat(2));
log2 = logninv(levels, parmhat(1),parmhat(2));
%Fit to Exponential Distribution
muhat_exp = expfit(y);
exp = expcdf(x,muhat_exp);
exp2 = expinv(levels,muhat_exp);
%Fit to Gamma Distribution
phat = gamfit(y);
gamma = gamcdf(x,phat(1),phat(2));
gamma2 = gaminv(levels, phat(1),phat(2));
%Fit to Weibull Distribution
muhatw = wblfit(y);
wei = wblcdf(x,muhatw(1),muhatw(2));
wei2 = wblinv(levels,muhatw(1),muhatw(2));
%Fit to GEV Distribution
parmhatg = gevfit(y);
gev = gevcdf(x,parmhatg(1),parmhatg(2),parmhatg(3));
gev2 = gevinv(levels, parmhatg(1),parmhatg(2),parmhatg(3));
%Fit to Uniform Distribution
[ahat,bhat] = unifit(y);
uni = unifcdf(x,ahat,bhat);
uni2 = unifinv(levels,ahat,bhat);
plot(x,norm,'k')
hold on
plot(x,log,'b')
hold on
plot(x,exp,'g')
hold on
plot(x,gamma,'y')
hold on
plot(x,wei,'m')
hold on
plot(x,gev,'c')
plot(x,uni,'r')
hold off
legend('Normal','Lognormal','Exponential','Gamma',...
'Weibull','GEV','Uniform','Location','best')
%Chi-squared Goodness-of-Fit test
%Normal
p = (0:0.2:1);
edgesN = norminv(p,muhat,sigmahat);
[hN,pN,stN]=chi2gof(y,'cdf',@(z)normcdf(z,muhat,sigmahat),...
'nparams',2,'edges',edgesN,'emin',2,'alpha',0.05);
%Lognormal
edgesL = logninv(p,parmhat(1),parmhat(2));
[hL,pL,stL]=chi2gof(y,'cdf',@(z)logncdf(z,parmhat(1),parmhat(2)),...
'nparams',2,'edges',edgesL,'emin',2,'alpha',0.05);
%Exponential
edgesE = expinv(p,muhat_exp);
[hE,pE,stE]=chi2gof(y,'cdf',@(z)expcdf(z,muhat_exp),...
'nparams',1,'edges',edgesE,'emin',2,'alpha',0.05);
%Gamma
edgesG = gaminv(p,phat(1),phat(2));
[hG,pG,stG]=chi2gof(y,'cdf',@(z)gamcdf(z,phat(1),phat(2)),...
'nparams',2,'edges',edgesG,'emin',2,'alpha',0.05);
%Weibull
edgesW = wblinv(p,muhatw(1));
[hW,pW,stW]=chi2gof(y,'cdf',@(z)wblcdf(z,muhatw(1)),...
'nparams',1,'edges',edgesW,'emin',2,'alpha',0.05);
%GEV
edgesGEV = gevinv(p,parmhatg(1),parmhatg(2),parmhatg(3));
[hGEV,pGEV,stGEV]=chi2gof(y,'cdf',@(z)gevcdf(z,parmhatg(1),...
parmhatg(2),parmhatg(3)),'nparams',3,'edges',edgesGEV,...
'emin',2,'alpha',0.05);
%Uniform
edgesU = unifinv(p,ahat,bhat);
[hU,pU,stU]=chi2gof(y,'cdf',@(z)unifcdf(z,ahat,bhat),...
'nparams',2,'edges',edgesU,'emin',2,'alpha',0.05);
%Calculate costs
xc = gev2;
%Abatement
ca = 5.2.*(xc-2.5).^12;
%Damage
cd = 985.*exp(-8.4.*(xc-9.3));
%Total
%ct = (ca + cd);
%find optimal cost...
%optcost=min(ct)
figure(3)
plot(risk,ca,'blue','linestyle','--','linewidth','1')
hold on
plot(risk,cd,'red','linestyle','--','linewidth','1')
hold on
plot(risk,ct,'black','linewidth','2')
xlabel('Level of Risk')
ylabel('Cost ($)');

Answers (1)

On line 28 you define:
exp = expcdf(x,muhat_exp);
Now you know why it is not a good idea to name variables the same name as MATLAB functions!
It is the same as this:
% First make some variables, unwisely choosing their names...
sin = 7;
cos = [4 5 6];
% Now check a common trig identity...
(sin(pi)^2 + cos(pi)^2)==1 % Error due to function masking...
I also note that you have named variables after other MATLAB functions. I hope you don't need to use these functions later. This is a bad habit you should break right away! All of these lines mask MATLAB functions...
norm = normcdf(x,muhat,sigmahat); % Masks the NORM function.
log = logncdf(x,parmhat(1),parmhat(2)); % Masks LOG function
log2 = logninv(levels, parmhat(1),parmhat(2)); % Masks LOG2 function.
gamma = gamcdf(x,phat(1),phat(2)); % Masks the GAMMA function.

Categories

Find more on Geology in Help Center and File Exchange

Asked:

on 7 May 2011

Community Treasure Hunt

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

Start Hunting!