Element by Element Multiplication Question

13 ビュー (過去 30 日間)
Natalie Rolf
Natalie Rolf 2018 年 4 月 16 日
コメント済み: Guillaume 2018 年 4 月 17 日
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 件のコメント
Star Strider
Star Strider 2018 年 4 月 17 日
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!

サインインしてコメントする。

回答 (1 件)

Guillaume
Guillaume 2018 年 4 月 16 日

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 件のコメント
Natalie Rolf
Natalie Rolf 2018 年 4 月 16 日
My apologies, I'm still learning.
Guillaume
Guillaume 2018 年 4 月 17 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by