How do I write code to plot ramp function? Is there any command for ramp?

The discrete function I want to plot is g[n] = ramp[n + 2]− 2ramp[n]+ ramp[n − 2]
The interval is -5<=n<=10
Thanks in advance.

 採用された回答

the cyclist
the cyclist 2015 年 3 月 26 日
There is no ramp function in MATLAB, but it could easily be coded as
function r = ramp(x)
r = max(0,x)

7 件のコメント

Ae lay
Ae lay 2015 年 3 月 26 日
編集済み: Ae lay 2015 年 3 月 26 日
Thanks for the reply. Then how to plot this equation g[n] = ramp[n + 2]− 2ramp[n]+ ramp[n − 2] ?
Ae lay
Ae lay 2015 年 3 月 26 日
編集済み: the cyclist 2015 年 3 月 26 日
n = -5:10;
p1 = ramp(n,1,-2); % for part 1
p2 = ramp(n,-2,0); % for part 2
p3 = ramp(n,1,2); % for part 3
Gh = p1+p2+p3; % equation of RAMP= the summation of 3 parts
stem(n,Gh);
The above code doesn't work.
the cyclist
the cyclist 2015 年 3 月 26 日
% Define the ramp function. See this page for details: http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
ramp = @(x) max(0,x);
% Another anonymous function defining g(x) as the sum of ramp functions.
g = @(x)(ramp(x+2) + 2*ramp(x)+ ramp(x-2));
% Define the input variable
n = -5:1:10;
% Plot
figure
plot(n,g(n),'.-')
Mohammad Shadman
Mohammad Shadman 2020 年 8 月 14 日
is this continous?
the cyclist
the cyclist 2020 年 8 月 14 日
Do you mean is g a continuous function?
Yes, g is continuous for real x in (-Inf,Inf).
Mohammad Shadman
Mohammad Shadman 2020 年 8 月 15 日
how will we plot for discreet?
the cyclist
the cyclist 2020 年 8 月 15 日
Take a look at the MATLAB Plot Gallery for ideas (and code) on how to plot.
If that doesn't help, please use more than 8 words to fully describe what you are trying to do, so that people don't waste time guessing at what you want.

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

その他の回答 (1 件)

ismael rashid
ismael rashid 2019 年 10 月 19 日

1 投票

clc
clear all
signal_start = input(' signal start value : ');
signal_end = input(' signal end value : ');
ramp_value = input(' ramp : ');
a = [ signal_start:signal_end];
b =mod(a,ramp_value);
plot(a,b)
% hope it helps you

カテゴリ

質問済み:

2015 年 3 月 26 日

コメント済み:

2020 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by