Set up a function corresponding to this graph

1 回表示 (過去 30 日間)
Danny Helwegen
Danny Helwegen 2019 年 2 月 10 日
コメント済み: Danny Helwegen 2019 年 2 月 11 日
hi, the following sketch was given to me and i need to set up a corresponding function. One problem, i have absolutely no clue, can somebody help me?
IMG-20190210-WA0001.jpg

採用された回答

Adam Danz
Adam Danz 2019 年 2 月 10 日
編集済み: Adam Danz 2019 年 2 月 10 日
In addition to the square() function (requires signal processing toolbox) and the heaviside() function (symbolic math toolbox), here's a much lower level, custom function that produces one step as is depicted in your image.
% Define step function
% * x is a vector, monotonically increasing
% * start: where the step should rise (scalar, member of x)
% * width: the width of the step (scalar, positive)
% * height: the height of the step (scalar, positive or negative)
% * base: the minimum base of the step (scalar)
stepFunc = @(x, start, width, height, base) ...
[zeros(1,sum(x < start)), 0, ones(1, sum(x >= start & x <= start+width)), 0, zeros(1, sum(x > start+width))] ...
* height + base;
% Calculate step function
% Define parameters
x = 0:1000;
start = 200;
width = 250;
height = 4;
base = 1.0;
y = stepFunc(x, start, width, height, base);
% Now we have to adjust x so that there are duplicate values where the step rises and falls
% in order to make it vertical
stepIdx = find(abs(diff(y)) == abs(height));
xIdx = [1:stepIdx(1), stepIdx(1):stepIdx(2)-1, stepIdx(2)-1:length(x)];
newX = x(xIdx);
% Plot results
figure
plot(newX, y)
ylim(ylim + [-1.1, 1.1]*range(ylim)) %incease y lim by 10%
  1 件のコメント
Danny Helwegen
Danny Helwegen 2019 年 2 月 11 日
Thanks, this was exactly what i needed

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

その他の回答 (1 件)

Mark Sherstan
Mark Sherstan 2019 年 2 月 10 日
Use the square function. The following should get you started:
t = linspace(0,3*pi)';
x = square(t);
plot(t,x)
ylim([-2,2])
  1 件のコメント
Danny Helwegen
Danny Helwegen 2019 年 2 月 11 日
thx for the answer, it was not exactly what i meant but still thanks

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

カテゴリ

Help Center および File ExchangeNetworks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by