Translation of a function

13 次查看(过去 30 天)
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))

采纳的回答

John D'Errico
John D'Errico 2015-10-14
编辑:John D'Errico 2015-10-14
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by