Element by Element Multiplication Question
조회 수: 23 (최근 30일)
이전 댓글 표시
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
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
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
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 Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!