plotting a function from anothe .m file , please help
1 次查看(过去 30 天)
显示 更早的评论
i am facing a problem plotting this function
.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));
-----------------------------
pleaseeee hellpppppp
0 个评论
采纳的回答
Matt Fig
2012-8-11
编辑:Matt Fig
2012-8-11
The problem is here:
clear all;
clc;
t = i:1:12;
Did you mean
t = 1:12;
10 个评论
Matt Fig
2012-8-11
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Probability Distributions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!