I am trying to plot a piecewise function that depends on Lambda, but Lambda depends on m which ranges from -0.9 to 1.0. I think I am having an issue storing the data points of H at different m and Lambda values, and then telling matlab to plot those stored values according to the conditional statement. Is this possible in matlab?
My code can be seen below
% Lambda is the lambda from Thwaites method
% where m is in the range: -0.9 < m < 1.0
m=linspace(-0.9, 1.0, 100)
Lambda = 0.45.*m/((5.*m) +1)
% H needs to be plotted as a piecewise function
% l is also a piecewise function
if (0<Lambda) && (Lambda<0.1)
H = 2.61 -3.75*Lambda + 5.24*Lambda^2
l = 0.22 + 1.57*Lambda -1.8*Lambda^2
elseif (-0.1<Lambda) && (Lambda<0)
H = 2.088 + (0.0731/(Lambda + 0.14))
l = 0.22 + 1.402*Lambda + ((0.018*Lambda)/(Lambda+0.107))
else
H = 0
l = 0
end
figure(2)
plot(m,H)
title('H vs m')
xlabel('m')
ylabel('H')
Any and all help is greatly welcome!
Thanks :)
Just do the if-else part using masks.
% Lambda is the lambda from Thwaites method
% where m is in the range: -0.9 < m < 1.0
m=linspace(-0.9, 1.0, 100)
Lambda = 0.45.*m/((5.*m) +1)
% allocate space for H & l (also else part in your code)
H = zeros(1, numel(m));
l = zeros(1, numel(m));
% if condition
cond1 = (Lambda > 0) & (Lambda < 0.1);
% attention to the piecewise exponential operator .^, I bet you mean that
H(cond1) = 2.61 - 3.75*Lambda(cond1) + 5.24*Lambda(cond1).^2;
l(cond1) = 0.22 + 1.57*Lambda(cond1) - 1.8*Lambda(cond1).^2;
% elseif condition
cond2 = (Lambda > -0.1) & (Lambda < 0);
H(cond2) = 2.088 + (0.0731 ./ (Lambda(cond2) + 0.14)); % division must be piecewise
l(cond2) = 0.22 + 1.402*Lambda(cond2) + ((0.018*Lambda(cond2)) ./ (Lambda(cond2)+0.107));
figure(2)
plot(m,H)
title('H vs m')
xlabel('m')
ylabel('H')