필터 지우기
필터 지우기

Error - matrix dimensions must agree

조회 수: 1 (최근 30일)
Joe Wheeler
Joe Wheeler 2020년 12월 28일
답변: Mischa Kim 2020년 12월 28일
Hi i cant seem to work out why this error keeps appearing in my code. thanks
x0 = 2;
y0 = 2;
x = [x0; y0];
iter = 0;
max_iter = 100;
tol = 0.1;
err = norm(f(x));
while iter <= max_iter && err >= tol
iter =iter + 1;
x = x - Jacob(x)\f(x);
err = norm(f(x));
end
function y = f(x)
y = (9-x.^2).^0.5;
f(x) = x.^2+y.^2-9;
g(x) = x.^2.*y+y.^3+1;
y = [f(x); g(x)];
end
function y = Jacob(x)
y = (9-x.^2).^0.5;
dfdx(x) = 2.*x;
dfdy(x) = 2.*y;
dgdx(x) = 2.*x.*y;
dgdy(x) = x.^2+3.*y.^2;
y = [dfdx(x) ,dfdy(x); dgdx(x) ,dgdy(x)];
end
  댓글 수: 1
Joe Wheeler
Joe Wheeler 2020년 12월 28일
apologies, the error is found here:
Error in systems_of_nonlinear_equations (line 14)
x = x - Jacob(x)\f(x);

댓글을 달려면 로그인하십시오.

답변 (1개)

Mischa Kim
Mischa Kim 2020년 12월 28일
Hi Joe, I think this is what you are trying to do:
x0 = 2;
y0 = 2;
x = [x0; y0];
iter = 0;
max_iter = 100;
tol = 0.001;
err = norm(f(x));
while iter <= max_iter && err >= tol
iter = iter + 1;
x = x - Jacob(x)\f(x);
err = norm(f(x));
end
function Y = f(X)
x = X(1); y = X(2);
f = x.^2+y.^2-9;
g = x.^2.*y+y.^3+1;
Y = [f; g];
end
function Y = Jacob(X)
x = X(1); y = X(2);
dfdx = 2.*x;
dfdy = 2.*y;
dgdx = 2.*x.*y;
dgdy = x.^2+3.*y.^2;
Y = [dfdx ,dfdy; dgdx,dgdy];
end
I will let you work through the code.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by