Making ramp and unit step function in MATLAB

163 visualizaciones (últimos 30 días)
Steven Franzkowiak
Steven Franzkowiak el 2 de Oct. de 2017
Respondida: Sajib Biswas Shuvo el 26 de Jul. de 2021
I am having trouble writing the code for these step functions. I would appreciate any help. I am using MATLAB version 9.3.0.701794 (R2017b)

Respuestas (2)

Robert U
Robert U el 5 de Oct. de 2017
Editada: Robert U el 5 de Oct. de 2017
Hello Steven Franzkowiak,
one solution is to write a function that behaves as the plotted function. One can argue about the points at t = 4 and t = 8 since they are not defined uniquely. I suppose f(t=4) = 0 and f(t=8) = -2.
The function can look like the following code. Matlab 2017b supports piecewise definition as can be found in one of the links posted by Ramnarayan Krishnamurthy.
function [ vec_out ] = hw_piecewise_R2014b( vec_in )
% support column/row vectors
if size(vec_in,1) > 1
vec_in = vec_in';
transpose_vec = 1;
else
transpose_vec = 0;
end
% everywhere else function returns zero
vec_out = zeros(1,size(vec_in,2));
% defined sections of function as seen from chart
vec_out(vec_in >= -6 & vec_in <= 0) = 5/6 * vec_in(vec_in >= -6 & vec_in <= 0) + 5;
vec_out(vec_in > 0 & vec_in <= 4) = -5/4 * vec_in(vec_in > 0 & vec_in <= 4) + 5;
vec_out(vec_in > 4 & vec_in <= 8) = -2;
% return vector according to input
if transpose_vec
vec_out = vec_out';
end
end
Having that function you must define a vector t and call the four versions:
t = -10:0.01:10;
f1 = hw_piecewise_R2014b(t);
figure; plot(t,f1)
f2 = hw_piecewise_R2014b(-t);
figure; plot(t,f2)
f3 = hw_piecewise_R2014b(t-4);
figure; plot(t,f3)
f4 = hw_piecewise_R2014b(2*t - 4)
figure; plot(t,f4)
f5 = hw_piecewise_R2014b(-t/2 - 4)
figure; plot(t,f5)
Kind regards,
Robert
  2 comentarios
Jan
Jan el 5 de Oct. de 2017
Posting full solutions of homework question has the drawback, that the OP cannot deliver this as his own solution anymore. Steven did not show, what he has tried so far and did not ask a specific question. Your solution is fine and according to the quality I would vote for it. But I'm afraid, it is a short-term help only for Steven.
Robert U
Robert U el 5 de Oct. de 2017
Thank you for your comment, Jan Simon. I decided to post my solution despite the drawback you describe.
In my opinion, the implicit link to logical indexing and functions are valuable for other readers as well and outweigh the drawback that Steven could accomplish his homework by copying the full solution.
Kind regards,
Robert

Iniciar sesión para comentar.


Sajib Biswas Shuvo
Sajib Biswas Shuvo el 26 de Jul. de 2021
This code does the job too. It uses the Symbolic Math toolbox.
syms t;
f_negative_t = 5*heaviside(-t) -5/6 * (-t) * heaviside(-t ) ...
+ 5/6 * (-t-6) * heaviside(-t -6);
f_positive_t = +5*heaviside(t) - 5/4 *t* heaviside(t) ...
- 2*heaviside(t-4) + 5/4*(t-4)*heaviside(t-4)...
+ 2*heaviside(t-8);
f = f_positive_t + f_negative_t;
fplot(f,[-7,10]);
xline(0), yline(0);
plot of the code

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by