plotting a function from anothe .m file , please help

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

 Risposta accettata

Matt Fig
Matt Fig il 11 Ago 2012
Modificato: Matt Fig il 11 Ago 2012
The problem is here:
clear all;
clc;
t = i:1:12;
Did you mean
t = 1:12;

10 Commenti

It suppose to be i = 1:1:12
can u please a propose a way how to plot at least HT.m function
So if you use
t = 1:12;
what happens?
Attempted to access HT_flag(1); index out of bounds because
??? Index exceeds matrix dimensions.
Error in ==> HT at 20 elseif HT_flag(T)>0
Error in ==> plotting2 at 8 plot(t,HT(t));
Matt Fig
Matt Fig il 11 Ago 2012
Modificato: Matt Fig il 11 Ago 2012
HT_flag is global. Using global variables is generally not necessary and frowned upon because of things like this!
You have a global variable that was not defined, or that is empty.
But there are other issues with your code. For example, in HT, T is a vector, but you are passing it to the IF as if it were a scalar. Did you mean to do this? See this article:
Specifically, look at the comment she refers to in the opening paragraph...
Tareq
Tareq il 11 Ago 2012
Modificato: Matt Fig il 11 Ago 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')
what about still give me error?
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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by