Receiving Errors On Matrix Dimensions
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I am unsure if I am making a simple mistake but I am receiving errors for matrix size within my equation code. Here is the code:
for
Z = 0.1
W_n = 2.5;
B = sqrt(1 - Z^2);
Theta = atan(B/Z);
c(t) = 1 - (1/B) * exp(1) .^ -(Z * W_n * t) * sin(B * W_n * t + Theta);
end
I am receiving these error messages:
Error using * Inner matrix dimensions must agree.
Error in FeedbackControlSystem (line 65) c(t) = 1 - (1/B) * exp(1) .^ -(Z * W_n * t) * sin(B * W_n * t + Theta);
Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.
Error in FeedbackControlSystem (line 65) c(t) = 1 - (1/B) * exp(1)^-(Z * W_n * t) * sin(B * W_n * t + Theta);
Any help would be greatly appreciated. Thank you!
댓글 수: 7
채택된 답변
Ameer Hamza
2018년 4월 24일
From your code in the comment section, It can be seen that you are making a couple of mistakes.
- t contain non-integers and thus can't be used to index c. In fact in this case you don't need to initialize c with zeros since it will not bring any speed up. Pre-allocation is only useful if your array size is changing in every loop iteration, here you can avoid that as given in below code snippet.
- Not taking care of matrix dimension when multiplying. In MATLAB * is for matrix multiplication and .* is for element-wise multiplication. (exp(1) .^ -(Z * W_n .* t)) is 1*360000 and (sin(B * W_n .* t + Theta))) is also 1*360000. Therefore you need .* operation between them.
t = linspace(1,3600,360000);
Z = 0.1;
W_n = 2.5;
B = sqrt(1 - Z^2);
Theta = atan(B/Z);
c = 1 - ((1/B) * (exp(1) .^ -(Z * W_n .* t)) .* (sin(B * W_n .* t + Theta)));
댓글 수: 4
Ameer Hamza
2018년 4월 24일
Yes, t and c have same dimensions. But the range of t is rather too large to see anything useful in plot. You might want to reduce the range of t.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
