I'm trying to solve two non-linear equations using fsolve

3 ビュー (過去 30 日間)
Sarah Elena Aiko Johnson
Sarah Elena Aiko Johnson 2022 年 10 月 31 日
回答済み: Torsten 2022 年 10 月 31 日
Hi I am trying to solve the two non-linear equations below with the initial x = 6 and y =6. I keep getting errors shown at the bottom that I dont understand how to fix. Any suggestions?
% Given: two non-linear simutaneous equations
% f(x,y) = 4 - y - 2x^2 and g(x,y) = 8 - y^2 - 4x
% Find:
% (1) Provide a function for storing f and g
% (2) Provide a test script to call the function storing the system
% of nonlinear equations and solve the two equations. Use xo = 6 and
% yo=6 as initial values.
% Solution:
% Let the initial conditions be plugged into the equations.
[x,fx] = fsolve(@fun,[6;6]);
% Set up the functions that will store the equations that can be
% called
function [f,g] = fun(x,y)
f = 4-y-2*x^2;
g = 8-y^2-4*x;
end
ERRORS I RECIEVED:
Not enough input arguments.
Error in Homework4_5>fun (line 36)
f = 4-y-2*x^2;
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Error in Homework4_5 (line 29)
[x,fx] = fsolve(@fun,[6;6]);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.

回答 (1 件)

Torsten
Torsten 2022 年 10 月 31 日
Variables and residuals must be put into one single vector:
[x,fx] = fsolve(@fun,[6;6])
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 2×1
1.0000 2.0000
fx = 2×1
1.0e-09 * -0.4071 -0.0670
% Set up the functions that will store the equations that can be
% called
function res = fun(z)
x = z(1);
y = z(2);
f = 4-y-2*x^2;
g = 8-y^2-4*x;
res = [f;g];
end

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by