필터 지우기
필터 지우기

Fsolve Intemidiate equations result

조회 수: 1 (최근 30일)
Rofl One
Rofl One 2021년 10월 21일
답변: Matt J 2021년 12월 1일
have a problem with MatLAB fsolve. How can I get the intermediate calculations for a given function? not only for the desired variable, but also for the equations included in the system for fsolve
fsolve code
function solve_syst ()
a_init = -2;
b = 5;
c = 6;
d = 8;
syst_eq = @(a) syst_3 (a, b, c, d);
%options = optimoptions('fsolve','Display','none', 'outputFcn')
[a_vih, fval] = fsolve (syst_eq, a_init);
end
first eq
function x1 = syst_1 (a, b) %first eq
x1 = a * 6 + 2 * b;
end
second eq
function x2 = syst_2 (c, d)%second eq
x2 = c * 4 - d * 2;
end
system for fsolve
function [prov, x1, x2] = syst_3 (a, b, c, d) %system for fsolve
x1 = syst_1 (a, b)
x2 = syst_2 (c, d)
prov = x1 - x2;
end
fsolve calculates intermediate values ​​x1 and x2 (visible in the command window), but I cannot get these values ​​separately to use in further calculations

답변 (2개)

Alan Weiss
Alan Weiss 2021년 10월 22일
You could use persistent variables along with assignin to write the variaible value to a workspace variable. Something like this:
function [prov, x1, x2] = syst_3 (a, b, c, d) %system for fsolve
persistent x1x2hist
x1 = syst_1 (a, b)
x2 = syst_2 (c, d)
x1x2hist = [x1x2hist;x1,x2];
assignin('base','x1x2hist',x1x2hist);
prov = x1 - x2;
end
That said, I think that it is a mistake to use fsolve to solve a linear system of equations. Instead, you should use the backslash command. You might also benefit from using the problem-based approach.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Matt J
Matt J 2021년 12월 1일
After solving, you can simply use the objective function that you have already written to obtain these values:
[a_vih, fval] = fsolve (syst_eq, a_init);
[~, x1, x2] = syst_eq(a_vih)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by