Element by Element Multiplication Question

23 vues (au cours des 30 derniers jours)
Natalie Rolf
Natalie Rolf le 16 Avr 2018
Commenté : Guillaume le 17 Avr 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 commentaire
Star Strider
Star Strider le 17 Avr 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!

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 16 Avr 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 commentaires
Natalie Rolf
Natalie Rolf le 16 Avr 2018
My apologies, I'm still learning.
Guillaume
Guillaume le 17 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands 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!

Translated by