Cost.m Function
조회 수: 1 (최근 30일)
이전 댓글 표시
Im doing this problem in class and it keeps telling me I have an error. Please help.
function [ J ] = cost( z )
a = z(1);
b = z(2);
c = z(3);
d = z(4);
e = z(5);
f = z(6);
w = [0:0.1:10]';
s = j*w;
Gideal = 1 * (w < 6);
G = a ./ ((s + b) *(s.^2 + c*s + d) *(s.^2 + e*s + f));
e = abs(Gideal) - abs(G);
J = sum(e .^ 2);
plot(w,abs(Gideal),w,abs(G));
pause(0.01);
end
댓글 수: 1
Geoff Hayes
2021년 3월 23일
Joe - please copy and paste the full error message to this question. Is it something similar to
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix.
or something else?
답변 (1개)
Vashist Hegde
2021년 3월 26일
DISCLAIMER: These are my own views and in no way depict those of MathWorks.
Hello Joe,
The error you seem to be gettting is:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
This is because in the line:
G = a ./ ((s + b) *(s.^2 + c*s + d) *(s.^2 + e*s + f));
you have used '*' operator to multiply the terms in the denominator. But in MATLAB '*' operator is considered to be matrix multiplication by default. And if we check the dimensions of each term you are trying tu multiply, this is the result:
Which are clearly incompatible for matrix multiplication.
Since you are trying to carry out element-wise multiplication, the right operator would be '.*'
hence the right expression would be:
G = a ./ ((s + b) .*(s.^2 + c*s + d) .*(s.^2 + e*s + f));
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!