Filter löschen
Filter löschen

Translation of a function

18 Ansichten (letzte 30 Tage)
Andrew Harris
Andrew Harris am 14 Okt. 2015
Bearbeitet: John D'Errico am 14 Okt. 2015
This has to be simpler than I'm making it. I have a function f(x) = 1-abs(x) where -1<x<1 and f(x) = 0 otherwise. I want to evaluate it for a translation f(x-2) and f(x-3). In algebra I know this should just take the initial function graph and move it to the right, however evaluating it in MATLAB changes the function from a triangle to a straight line, and over the wrong range. Any ideas what I'm missing?
f = @(x) 1-abs(x)
x = linspace(-1, 1)
figure
plot(x, f(x))
x1 = linspace(1, 3);
x2 = linspace(2, 4);
figure
plot(x1, f(x-2))
hold on
plot(x2, f(x-3))

Akzeptierte Antwort

John D'Errico
John D'Errico am 14 Okt. 2015
Bearbeitet: John D'Errico am 14 Okt. 2015
Try this modification instead. You almost had it right.
f = @(x) max(1-abs(x),0);
ezplot(f,-3,3)
ezplot(@(x) f(x-1),-3,3)
In your original function, when you translated it, you were seeing only one half of the abs function, and you were allowing it to go to -inf. The max that I added cuts off the function when it wants to go negative.

Weitere Antworten (0)

Kategorien

Mehr zu Modeling finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by