how we can define piecewise function in matlab?

in maple we can use comamnd:(
f:= piecewise(0 <= t and t <= T1, f1, T1 < t and t < T2, 0, T2 <=t and t <= T3, f1, T3 < t and t < T4, 0)
but in matlab i can not define this function.
please give me som information ...
thanks

 채택된 답변

Sally Al Khamees
Sally Al Khamees 2016년 12월 23일
편집: Sally Al Khamees 2017년 2월 21일

2 개 추천

If you have R2016b and the Symbolic Math Toolbox installed, you can just use the piecewise function:

추가 답변 (3개)

Walter Roberson
Walter Roberson 2013년 10월 12일

2 개 추천

You cannot define that as a function in MATLAB. MATLAB functions need to have a defined value for the case where none of the conditions hold. If you were absolutely sure that one of the conditions will hold you would rewrite the Maple function without the final condition, as
piecewise(0 <= t and t <= T1, f1, T1 < t and t < T2, 0, T2 <=t and t <= T3, f1, 0)
This would be
function y = f(t, T1, T2, T3, f1)
y = zeros(size(t));
idx1 = 0 <= t & t <= T1;
y(idx1) = f1;
idx2 = T1 < t & t < T2;
y(idx2) = 0;
idx3 = T2 <= t & t <= T3;
y(idx3) = f1;
y(~(idx1 | idx2 | idx3)) = 0;
end

댓글 수: 4

ebi
ebi 2013년 10월 12일
hello dear walter... thanks for your answer , but i think i couldn't express clearly. i want define a piecewise function like as picture below that t is variable and we have f(t)= picture below and i want to plot it and do fourier illustration of it and use fft function on it ant plot them.
You can only do fourier transforms symbolically, using the symbolic toolbox. For numeric values, you need to use fft (fast fourier transform) or the like.
If you want to use the symbolic toolbox to create the MuPAD equivalent of the Maple function, you need to change the syntax very slightly:
f_generic = sym('piecewise((0 <= t and t <= T1, f1), (T1 < t and t < T2, 0), (T2 <=t and t <= T3, f1), (T3 < t and t < T4, 0))');
Note: possibly the inner () should be [] instead.
Before using you would want to
f = subs(f_generic, {'T1', 'T2',, 'T3', 'f1'}, {0.02, 0.287, 0.307, 0.335});
Note that the result would be a symbolic formula, not a function. To get the function version, use
f_generic = sym('t -> piecewise((0 <= t and t <= T1, f1), (T1 < t and t < T2, 0), (T2 <=t and t <= T3, f1), (T3 < t and t < T4, 0))');
ebi
ebi 2013년 10월 12일
thanks a lot i will try it ...
Note: my later experiments showed that using sym('t->...') cannot possibly create a symbolic function.

댓글을 달려면 로그인하십시오.

sixwwwwww
sixwwwwww 2013년 10월 12일

0 개 추천

Dear ebi, You need something like this if I understood correctly:
syms f f1
t = input('Enter value of t: ');
% Assuming values of T1, T2 and T3 as follows
T1 = 10;
T2 = 20;
T3 = 30;
if ((t >= 0 && t <= T1) || (t >= T2 && t <= T3))
f = f1; % here you can define f1 as you wish
else
f = 0;
end
Is it ok?

댓글 수: 3

ebi
ebi 2013년 10월 12일
hello dear sixww... thanks for your answer , but i think i couldn't express clearly. i want define a piecewise function like as picture below that t is variable and we have f(t)= piecewise function and i want to plot it and do fourier illustration of this function and use fft function on it ant plot them.
sixwwwwww
sixwwwwww 2013년 10월 12일
Do you input 't' as a time vector? and get f(t) for which you can calculate fft? Is it right?
ebi
ebi 2013년 10월 13일
i plot f(t) from [0..0.336]s and for that time period i have to calculate the integral of (f(t).exp(-i.w.t)dt)=F(w) and then plot F - w .

댓글을 달려면 로그인하십시오.

카테고리

질문:

ebi
2013년 10월 12일

답변:

2019년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by