MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Asked by Tareq on 11 Aug 2012

i am facing a problem plotting this function

objfun244.m

```function f=objfun244(tp)
% The objective function: total cost per unit time if replacement interval is tp
% Assumption: The smallest time unit is week.
```
```% f: total cost per unit time
% tp: Length of the time interval in Weeks (preventive replacement interval)
```
```global Cp Cf   % cost of preventive replacement and failure replacement, respectively
T =ceil(tp);
```
```% f: total cost per unit time C_tp
f=(Cp+Cf*HT(T))/tp;
```
```% % Test
% objfun244(4)
```

then on another .m file which is called plotting2.m here is the code i wrote

```clear all;
```
```clc;
```
```t = i:1:12;
```
```plot(t,objfun244(t));
```
```grid on
```
```xlabel('t')
```
```ylabel('COST')
```

---------------------------------------

there is another function called H T , here is its code

```function H=HT(T)
% The recursive function H(T)
% Calculate the Expected number of failures in (0,T], for Weibull
% Distribution
% Assumption: T's unit is week, the smallest time unit.
```
```% H: the Expected number of failures in (0,T]
% T: length of the time interval in weeks (preventive replacement interval)
% T is a nonnegative integer: 0, 1, 2, 3, ...
```
```global Al Be % Alpha and Beta for the Weibull distribution, respectively  % #####
global HT_vec HT_flag % Calculated H(T) values: HT_flag(T)=1 if it has been calculated
```
```H=0;   % H(0)=0: initial value
```
```if T<0.001
```
```%     H=0;   % If T=0, H(T)=0;
```
```elseif HT_flag(T)>0
% If HT(T) has been calculated, used the saved value
H=HT_vec(T);  % Assign the value from HT_vec
```
```else
% Otehrwise, recursively calculate H(T)
% i: time from 0 to T-1
```
```      for i=0:(T-1)
H=H+(1+HT(T-i-1))*(wblcdf(i+1,Al,Be)-wblcdf(i,Al,Be)); % #####
end
% Save the calculated H(T) value
HT_vec(T)=H;
HT_flag(T)=1;
end```

----------------------------------------------------

```i got error : ??? Subscript indices must either be real positive integers or logicals.
```
```Error in ==> HT at 20
elseif HT_flag(T)>0
```
```Error in ==> objfun244 at 12
f=(Cp+Cf*HT(T))/tp;
```
```Error in ==> plotting2 at 11
plot(t,objfun244(t));
```

-----------------------------

## Products

No products are associated with this question.

Answer by Matt Fig on 11 Aug 2012
Edited by Matt Fig on 11 Aug 2012

The problem is here:

```clear all;
```
```clc;
```
```t = i:1:12;
```

Did you mean

```t = 1:12;
```

Tareq on 11 Aug 2012
```function H=HT2(T)
% The recursive function H(T)
% Calculate the Expected number of failures in (0,T], for Weibull
% Distribution
% Assumption: T's unit is week, the smallest time unit.
```
```% H: the Expected number of failures in (0,T]
% T: length of the time interval in weeks (preventive replacement interval)
% T is a nonnegative integer: 0, 1, 2, 3, ...
```
```global Al Be % Alpha and Beta for the Weibull distribution, respectively  % #####
global HT_vec HT_flag % Calculated H(T) values: HT_flag(T)=1 if it has been calculated
```
```T= 2:1:12;
H=0;   % H(0)=0: initial value
for i2 = 1:length(T)
if HT_flag(T(i2))>0
% If HT(T) has been calculated, used the saved value
H=HT_vec(T(i2));  % Assign the value from HT_vec
```
```else
% Otehrwise, recursively calculate H(T)
% i: time from 0 to T-1
```
```      for i=0:(T(i)-1)
H=H+(1+HT2(T(i)-i-1))*(wblcdf(i+1,Al,Be)-wblcdf(i,Al,Be)); % #####
end
% Save the calculated H(T) value
HT_vec(T(i))=H;
HT_flag(T(i))=1;
end
end```
```plot(T,HT2(T));
```
```grid on
```
```xlabel('t')
```
```ylabel('COST')
```
Tareq on 11 Aug 2012

what about still give me error?

Matt Fig on 11 Aug 2012

Please use the {} Code button when pasting code. And be specific in your questions. What did the error message say? I bet it is here:

```for i=0:(T(i)-1)
```

In this line you are defining i for the first time (masking MATLAB's imaginary unit, I might add), so you cannot refer to it in the same line.