フィルターのクリア

handles for functions of several variables

12 ビュー (過去 30 日間)
Stina Ravdna Lorås
Stina Ravdna Lorås 2021 年 3 月 18 日
Im trying to make function handles that make it possible to calculate functions with different values for x1 and x2, but I cannot make it work. Please help?
(This is not the entire code, and it is split into two files)
syms x1 x2
x0 = [-1; -1];
f = @f_function;
J = @J_function;
X = NewtonsMethod(f, J, x0, lim, N)
function f = f_function(x)
f=[(x1*x2-2);
((x1.^4/4) + (x2.^3/3) -1)];
end
function J = J_function(x)
J = jacobian(f, [x1,x2]);
end
File:NewtonsMethod
xn = x0; % initial estimate
n = 1; % iteration number
fn = f(xn); % save calculation
iterate = norm(fn,Inf) > tol;
while iterate
xn = xn - J(xn)\fn;
n = n+1;
X(:,n) = xn;
fn = f(xn);

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 19 日
syms x1 x2
x0 = [-1; -1];
fsym = f_function(x1,x2);
f = matlabFunction(fsym, 'vars', {[x1, x2]})
f = function_handle with value:
@(in1)[in1(:,1).*in1(:,2)-2.0;in1(:,1).^4./4.0+in1(:,2).^3./3.0-1.0]
J = matlabFunction(jacobian(fsym), 'vars', {[x1, x2]})
J = function_handle with value:
@(in1)reshape([in1(:,2),in1(:,1).^3,in1(:,1),in1(:,2).^2],[2,2])
X = NewtonsMethod(f, J, x0, lim, N)
Unrecognized function or variable 'lim'.
function f = f_function(x1,x2)
f=[(x1*x2-2);
((x1.^4/4) + (x2.^3/3) -1)];
end
  1 件のコメント
Stina Ravdna Lorås
Stina Ravdna Lorås 2021 年 3 月 19 日
Thank you! :)

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2021 年 3 月 18 日
編集済み: Jan 2021 年 3 月 18 日
Do you get an error message?
What is x1 and x2 in your functions "f_function" and "J_function"? Do you mean: x(1) and x(2) ?
Be careful with the unary minus in vectors:
f=[(x1*x2-2);
((x1.^4/4) + (x2.^3/3) -1)];
% ^
This can confuse the parser. See:
[1 1] % [1, -1]
[1 -1] % [1, -1]
[1 - 1] % [2]
  2 件のコメント
Stina Ravdna Lorås
Stina Ravdna Lorås 2021 年 3 月 19 日
x1 and x2 is x(1) and x(2) = x.
I used parathesis to avoid the misunderstanding between row or function.
I've now changed my handles into this:
f = @(x)[(x(1)*x(2)-2);
((x(1).^4/4)+(x(2).^3/3)-1)];
J = @(x)jacobian(f, x);
But it is still wrong...
Stina Ravdna Lorås
Stina Ravdna Lorås 2021 年 3 月 19 日
Thank you for answering though! :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by