User defined function to output matrix

I am in need of some help with a user defined function. I have it so it will output a column array of zeros however I need to function to fill the array dependant on a number of conditions and I have no idea how to get from where I am to where I need to be.
function [Q] = O_R_B(H)
%Operating rule B for varying dam heights
% Determining the usage rates for the dam for different dam heights
H_max = 30.9; %dam height in m (scaled 1:300)
Q_out = 138; %normal output rate
Q = zeros(length(H),1);
for k = 1:length(H)
if H >= 1*H_max;
Q = string('issue warning');
elseif H >= 0.9*H_max;
Q(k) = 4 * Q_out;
elseif H < 0.9*H_max & H>= 0.85*H_max;
Q(k) = 2* Q_out;
elseif H < 0.85*H_max & H>= 0.4*H_max;
Q(k) = Q_out;
elseif H < 0.4*H_max & H>= 0.2*H_max;
Q(k) = 0.5*Q_out;
else H< 0.2*H_max;
Q(k) = 0;
end
end
end

 采纳的回答

You need to index H also in your code. E.g.,
if H(k) >= 1*H_max;
error('H too large');
elseif H(k) >= 0.9*H_max;
Q(k) = 4 * Q_out;
:
etc

6 个评论

thank you this has gotten me closer to the answer however it is only outputting values in the first and last location (for testing purposes the range I am using is 10:10:30) and the centre value is being produced as a 0. Thank you for your assistance with this.
ans =
69
0
552
Please post your current code. Did you change both of the H's into H(k)'s in the middle tests?
Also, since these are scalar tests you are doing, it would be more appropriate to use the && operator instead of the & operator.
sorry should have made sure to post my code in my previous comment.
function [Q] = O_R_B(H)
%Operating rule B for varying dam heights
% Determining the usage rates for the dam for different dam heights
H_max = 30.9; %dam height in m (scaled 1:300)
Q_out = 138; %normal output rate
Q = zeros(length(H),1);
for k = 1:length(H)
if H(k) >= 1*H_max;
Q = string('issue warning');
elseif H(k) >= 0.9*H_max;
Q(k) = 4 * Q_out;
elseif H(k) < 0.9*H_max & H>= 0.85*H_max;
Q(k) = 2* Q_out;
elseif H(k) < 0.85*H_max & H>= 0.4*H_max;
Q(k) = Q_out;
elseif H(k) < 0.4*H_max & H>= 0.2*H_max;
Q(k) = 0.5*Q_out;
else H(k) < 0.2*H_max;
Q(k) = 0;
end
end
end
This is where I am at currently. The other thing I also need to achieve is that if a value greater than H_max occurs i need an output of issue warning in the matrix as well and I have no clue on how to achieve this.
You didn't change both of the H's to H(k)'s. E.g., this line
elseif H(k) < 0.9*H_max & H>= 0.85*H_max;
should be this instead:
elseif H(k) < 0.9*H_max & H(k) >= 0.85*H_max
And, to simplify things, since your tests cover the entire range of 0 to H_max, you don't even need the < part of the tests. You know you will pass that part of the test guaranteed because you just failed the previous test. So you only need the >= part of the test. E.g.,
if H(k) >= 1*H_max;
Q = string('issue warning'); % <-- YOU NEED TO CHANGE THIS!
elseif H(k) >= 0.9*H_max;
Q(k) = 4 * Q_out;
elseif H(k) >= 0.85*H_max;
Q(k) = 2* Q_out;
elseif H(k) >= 0.4*H_max;
Q(k) = Q_out;
elseif H(k) >= 0.2*H_max;
Q(k) = 0.5*Q_out;
else
Q(k) = 0;
end
You need to change your warning line. You can't mix a string in with your double data. I would either put a NaN there or maybe just error() the function at that point.
You sir are a life saver I have been looking at this for 2 days trying to figure it out.
You could issue a warning and fill the element with NaN. E.g.,
if H(k) >= 1*H_max;
Q(k) = NaN;
warning(sprintf('H(%d) element too large',k));

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by