solving two-variable matrix function

조회 수: 3 (최근 30일)
Dmitry Solomatin
Dmitry Solomatin 2022년 12월 14일
댓글: Jiri Hajek 2022년 12월 14일
Hi,
so assume I have a function like this:
f = [x(1) + x(2) ; x(2) * x(1)]
and my x = [2; -2]
how can I solve it? feval doesnt work for some reason ("Dimensions of arrays being concatenated are not consistent)

답변 (1개)

Jiri Hajek
Jiri Hajek 2022년 12월 14일
Hi this can be done using inline function very efficiently like this:
fh=@(x,y)[x+y,x*y];
fh(2,-2)
ans = 1×2
0 -4
  댓글 수: 2
Dmitry Solomatin
Dmitry Solomatin 2022년 12월 14일
Thanks it now works, but for some reason this gives me a 1x3 matrix and it should only 1x2?
fh = @ (x,y) [2*x- 400*x*(-x^2+y) -2, 200 * (y-x^2)];
fh(2,-2)
Jiri Hajek
Jiri Hajek 2022년 12월 14일
This is because Matlab interprets space within brackets as a separator of vector elements, same as a comma. You can eithertake away all the spaces or put your expressions into parentheses. That way, you can forget about the meaning of spaces:
fh = @ (x,y) [(2*x- 400*x*(-x^2+y) -2), (200 * (y-x^2))]
fh = function_handle with value:
@(x,y)[(2*x-400*x*(-x^2+y)-2),(200*(y-x^2))]
fh(2,-2)
ans = 1×2
4802 -1200

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by