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 CenterFile Exchange에서 Systems 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