Element by Element Multiplication Question

13 views (last 30 days)
Natalie Rolf
Natalie Rolf on 16 Apr 2018
Commented: Guillaume on 17 Apr 2018
Hey, So I have this program
clear; clc;
figure(1); clf;
x=linspace(0,5);
y1=(1-exp(-x)).*cos(4*x)-(0.2*exp(-x)).*sin(4*x);
y2=(1-exp(-2*x)).*cos(4*x)-(0.2*exp(-2*x)).*sin(4*x);
y3=(1-exp(-5*x)).*cos(4*x)-(0.2*exp(-5*x)).*sin(4*x);
plot(x,y1, '-b'); grid on; hold on;
plot(x,y2, '--r');
plot(x,y3, '-.m');
title('Step response vs. \sigma');
xlabel('time [sec]');
ylabel('x(t) [cm]');
text(1.5,0.5,'1-e^{-\sigmat}cos(\omega_dt)-0.2e^{-\sigmat}sin(\omega_dt)',...
'FontWeight', 'bold', 'FontSize',12);
ylim([0,1.5]);
and I want to do the element by element multiplication of y1,y2,and y3. However when I use the . operator I get an error message. I'm certain it's because of there being multiple instances of 'x' throughout the functions, but I'm not sure how to correct this. I have tried moving the dot operator around after each 'x'.
Error Message: Error using * Inner matrix dimensions must agree.
Error in MAE1090_HW8_4 (line 8) y1=(1-exp(-x))*cos(4*x)-(0.2*exp(-x))*sin(4*x);
Thanks!
  1 Comment
Star Strider
Star Strider on 17 Apr 2018
The code you posted runs without error in R2018a.
I would remove the clf call after declaring figure(1), since that clears the figure object you just created!

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 16 Apr 2018

Please copy and paste your code rather than using screenshot. We can't copy and paste out of a screenshot, hence we can't easily try your code. Also when you say you get an error message, gives the full text of the error message so we don't have to guess what the issue could be.

As it is, it's unclear what you mean by the . operator. If it's . on its own, then yes you'll get an error since element-wise multiplication is .*.

What i don't understand is why you don't get an error for your three y? = lines. They all require element-wise multiplication to work:

y1 = (1-exp(-x)) .* cos(4*x) - (0.2*exp(-x)) .* sin(4*x);

same for y2 and y3.

  2 Comments
Natalie Rolf
Natalie Rolf on 16 Apr 2018
My apologies, I'm still learning.
Guillaume
Guillaume on 17 Apr 2018
No problem. It's much easier to diagnose the problem after your edit.
It looks like the only problem was on the creation of y1, y2 and T3. Now that you've fixed it, the code should run fine.

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!