Anonymous function of a series of nonlinear equations to accept vector input

2 次查看(过去 30 天)
I have a series of nonlinear equations, written as an anonymous function handle that outputs a vector solution of each function and Im trying to input a vector of x1,x2,x3. For the sake of the the error in my code I simplified it dramatically, however I cannot seem to get the function to output my desired results.
f= @(x1,x2,x3) [x1.^3;x2.^2;x3.^2]
f = function_handle with value:
@(x1,x2,x3)[x1.^3;x2.^2;x3.^2]
x=[1;1;1]
x = 3×1
1 1 1
f(x')
Not enough input arguments.

Error in solution>@(x1,x2,x3)[x1.^3;x2.^2;x3.^2] (line 1)
f= @(x1,x2,x3) [x1.^3;x2.^2;x3.^2]
"Invalid expression. When calling a function or indexing a variable, use parentheses.
Otherwise, check for mismatched delimiters"
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-9-21
The function you have defined expects 3 inputs -
f= @(x1,x2,x3) [x1.^3;x2.^2;x3.^2]
f = function_handle with value:
@(x1,x2,x3)[x1.^3;x2.^2;x3.^2]
But you have provided only 1 input, so it gives error
x=[1;1;1]
x = 3×1
1 1 1
f(x')
Not enough input arguments.

Error in solution>@(x1,x2,x3)[x1.^3;x2.^2;x3.^2] (line 1)
f= @(x1,x2,x3) [x1.^3;x2.^2;x3.^2]
I don't know how you about this error - "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters"

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2023-9-21
Either:
f= @(x1,x2,x3) [x1.^3;x2.^2;x3.^2];
f = function_handle with value:
@(x1,x2,x3)[x1.^3;x2.^2;x3.^2]
x=[1;1.2;1.3];
x = 3×1
1.0000 1.2000 1.3000
f(x(1),x(2),x(3))
ans = 3×1
1.0000 1.4400 1.6900
or:
f= @(x) [x(1).^3;x(2).^2;x(3).^2];
f = function_handle with value:
@(x)[x(1).^3;x(2).^2;x(3).^2]
x=[1;1.2;1.3];
x = 3×1
1.0000 1.2000 1.3000
f(x')
ans = 3×1
1.0000 1.4400 1.6900
should work.
.
  2 个评论
Jordan Duffy
Jordan Duffy 2023-9-21
Thank you, it worked! The latter is what I was looking for as I was hoping to avoid manually inputting into f, so that the solver is general for any nonlinear equation input function handle.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Computations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by