Defining and plotting a piecewise function

Hi everybody!
I am trying to deifne and plot the function below in matlab, without success. Can anybody point how I could plot this function?
for 0 <= T < Tb, Se(T) = ag*S*?*F0*[(T/Tb) + (1/?*F0)*(1 - T/Tb)]
for Tb <= T < Tc, Se(T) = ag*S*?*F0
for Tc <= T < Td, Se(T) = ag*S*?*F0*(Tc/T)
for Td <= T, Se(T) = ag*S*?*F0*(Tc*Td/(T^2))
Note: I have defined all values of ag, S, n, F0, Tb, Tc, Td in my code.
function S = f(T);
S=0;
if T >= 0 & T < Tb
S = ag*S*n*F0*[(T/Tb) + (1/n*F0)*(1 - T/Tb)];
elseif T >= Tb & T < Tc
S = ag*S*n*F0;
elseif T >= Tc & T < Td
S = ag*S*n*F0*(Tc/T);
else T>=Td
S = ag*S*n*F0*(Tc*Td/(T.^2));
end
end
Thanks!

 采纳的回答

Another option:
S = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc*Td/(T.^2))).*(T >= Tc & T < Td);
If all the other values are scalars, that should work.
.

8 个评论

Thank you! But when trying to plot it, I am getting an empty figure.
Is this correct?
f = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc/T)).*(T >= Tc & T < Td) + ag*S*n*F0*(Tc*Td/(T.^2)).*(T>=Td);
T = linspace(0, 10, 500);
figure(1);
plot(T,f(T));
Thanks
My pleasure.
When ‘T’ appears in the denominator, it is necessary to use element-wise division.
This:
ag*S*n*F0*(Tc*Td/(T.^2))
should be:
ag*S*n*F0*(Tc*Td./(T.^2))
↑ ← ELEMENT-WISE DIVISION
so the corrected function is:
S = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc*Td./(T.^2))).*(T >= Tc & T < Td);
That should work. (I did not see that earlier.)
.
Thanks again :)
There is a problem with the plot function for some reason, I don't know why.
I get this error in the command window:
Error in Exercise0 (line 25)
plot(T,f(T));
Is the code to plot the function below correct?
f = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc/T)).*(T >= Tc & T < Td) + ag*S*n*F0*(Tc*Td./(T.^2)).*(T>=Td);
T = linspace(0, 10, 500);
figure(1);
plot(T,f(T));
Thanks in advance :)
My pleasure.
Is there more to the error message?
What are the variables (ag, S, n, F0, Tb, Tc, Td) inside the function?
.
This is the error:
Error using /
Matrix dimensions must agree.
Error in
Exercise0>@(T)(ag*S*n*F0*((T/Tb)+(1/n*F0)*(1-T/Tb))).*(T>=0&T<Tb)+(ag*S*n*F0).*(T>=Tb&T<Tc)+(ag*S*n*F0*(Tc/T)).*(T>=Tc&T<Td)+ag*S*n*F0*(Tc*Td./(T.^2)).*(T>=Td)
(line 22)
f = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T <
Tc) + (ag*S*n*F0*(Tc/T)).*(T >= Tc & T < Td) + ag*S*n*F0*(Tc*Td./(T.^2)).*(T>=Td);
Error in Exercise0 (line 25)
plot(T,f(T));
>>
This is the complete code, maybe it might help to find the error :)
close all;
clear
clc
g=9.81;
ag= 0.177*g;
damping = 5;
F0 = 2.544;
Tcstar = 0.281;
St = 1.0;
Cc= 1.25*(Tcstar).^-0.5;
Ss = 2.4 - 1.5*(F0*ag/g);
S = Ss*St;
Tc = Cc*Tcstar;
Tb = Tc/3;
Td = 4*ag/g + 1.6;
n = (10/(5+damping)).^0.5;
f = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc/T)).*(T >= Tc & T < Td) + ag*S*n*F0*(Tc*Td./(T.^2)).*(T>=Td);
T = linspace(0, 10, 500);
figure(1);
plot(T,f(T));
Same problem, different term.
Try this:
f = @(T) (ag*S*n*F0*((T/Tb) + (1/n*F0)*(1 - T/Tb))).*(T >= 0 & T < Tb) + (ag*S*n*F0).*(T >= Tb & T < Tc) + (ag*S*n*F0*(Tc./T)).*(T >= Tc & T < Td) + ag*S*n*F0*(Tc*Td./(T.^2)).*(T>=Td);
.
Perfect! Thanks a lot! Have a nice day
As always, my pleasure!
You, too!

请先登录,再进行评论。

更多回答(1 个)

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by