How to multiply function by the unit step function

19 次查看(过去 30 天)
I am trying to multiply a function, f(t), by the unit step function, u(t), and consider the solution another function g(t), i.e. g(t)=f(t)*u(t) then plot as follows:
if true
%clear all
t=-10:0.05:20;
u(t>=0)=1;
f=inline('(exp(-.2*t).*sin(pi*t))','t');
g=inline('f(t).*u(t)','t');
figure(4)
plot(t,g(t))
grid
axis([-10 20 -1 1]);
ylabel('g(t)')
xlabel('Time (sec)')
title('g(t)=f(t)*u(t)')
hold off;
end

回答(1 个)

Star Strider
Star Strider 2017-2-13
I would use anonymous functions rather than inline functions (that will likely disappear in the not distant future).
This works:
t=-10:0.05:20;
u = @(t) +(t>=0); % Create A Function For ‘u(t)’
f = @(t) (exp(-.2*t).*sin(pi*t));
g = @(t) f(t).*u(t);
figure(4)
plot(t,g(t))
grid
axis([-10 20 -1 1]);
ylabel('g(t)')
xlabel('Time (sec)')
title('g(t)=f(t)*u(t)')
hold off;
My code for ‘u(t)’ uses your logical operator to create a series of logical true or 1, and putting a ‘+’ before it converts it from a series of logical 1 to double. (This is a little trick I learned from one of Stephen Cobeldick’s answers.)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by