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!

 採用された回答

Star Strider
Star Strider 2020 年 4 月 16 日

0 投票

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 件のコメント

Majd Salhab
Majd Salhab 2020 年 4 月 16 日
編集済み: Majd Salhab 2020 年 4 月 16 日
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
Star Strider
Star Strider 2020 年 4 月 16 日
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.)
.
Majd Salhab
Majd Salhab 2020 年 4 月 16 日
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 :)
Star Strider
Star Strider 2020 年 4 月 16 日
My pleasure.
Is there more to the error message?
What are the variables (ag, S, n, F0, Tb, Tc, Td) inside the function?
.
Majd Salhab
Majd Salhab 2020 年 4 月 16 日
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));
Star Strider
Star Strider 2020 年 4 月 16 日
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);
.
Majd Salhab
Majd Salhab 2020 年 4 月 16 日
Perfect! Thanks a lot! Have a nice day
Star Strider
Star Strider 2020 年 4 月 16 日
As always, my pleasure!
You, too!

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 16 日

0 投票

See piecewise(): https://www.mathworks.com/help/releases/R2020a/symbolic/piecewise.html. You can use it with symbolic toolbox to define a piecewise function.

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by