# How to plot this signal x(𝑡) = (𝑡 + 2)𝑢(𝑡 + 2) − 2𝑡𝑢(𝑡) + (2(𝑡 − 4) + 2)𝑢(𝑡 − 4)?

85 vues (au cours des 30 derniers jours)
Olivia DeBenedictis le 26 Avr 2021
I just am unsure of the correct syntax for plotting a signal with unit step. This is what I have so far. It gives me an error saying incorrect dimensions but I don't know where to put . for multiplying.
clear all; close all; clc;
syms t t0
u(t) = piecewise(t<t0, 0, t>=t0, 1);
t = -10:1:10;
xt = (t+2)*u(t+2)-(2*t*u(t))+((2*(t-4)+2)*u(t-4));
plot(t,xt);
axis([0,40,-2,2])
title('x(t) = (t+2)*u(t+2)-(2*t*u(t))+((2*(t-4)+2)*u(t-4))')
xlabel('t')
ylabel('x(t)')
grid;
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Clayton Gotberg le 26 Avr 2021
Modifié(e) : Clayton Gotberg le 26 Avr 2021
One error is in the line
xt = (t+2)*u(t+2)-(2*t*u(t))+((2*(t-4)+2)*u(t-4));
and it is happening because of problems with the size of arrays you're calling. For example, in the first part:
(t+2)*u(t+2)
(t+2) % Adds 2 to each element in t, a 1x21 double
u(t+2) % output of u function on t+2, a 1x21 double
A*B % matrix multiplication - the number of columns in A must equal
% the number of rows in B
When you use *, MATLAB assumes you want matrix multiplication. If you want elements in A to be multiplied by elements in the same place in B, use element-wise multiplication (.*) instead.
To answer your question, anywhere that you want to use element-wise multiplication needs a .* instead of a *
##### 2 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Olivia DeBenedictis le 28 Avr 2021
Thank you so much for your help!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!