How to use fsolve to solve all the equations?

조회 수: 14 (최근 30일)
fayu
fayu 2024년 11월 18일
댓글: Matt J 2024년 11월 18일
%For the following equations want to solve all of its solutions, using solve to solve the time is too long, but using fsolve to solve I wrote the following program, but feel that the solution is not quite right, given too many results
function [F] = modulation_equation(X)
a=X(1);
c=X(2);
F(1)= 0.1096622461e0 * a ^ 6 - 0.4223705634e-1 * a ^ 8 + 0.4066962405e-2 * a ^ 10 - 0.1613995833e-2 * c ^ 2 * a ^ 4 + 0.5938649525e-5 * c ^ 4 * a ^ 2 - 0.3163847188e7 * c ^ 2 * a ^ 8 + 0.8689234851e5 * c ^ 4 * a ^ 6 + 0.9705023202e3 * c ^ 6 * a ^ 4 - 0.8469105945e0 * c ^ 2 * a ^ 6 + 0.1203014830e-1 * c ^ 4 * a ^ 4 - 0.4506937871e-4 * c ^ 6 * a ^ 2 + 0.8083313632e1 * c ^ 8 * a ^ 2;
F(2)= -0.6970477491e12 * a ^ 12 + 0.1914345842e11 * c ^ 2 * a ^ 10 + 0.2138367102e9 * c ^ 4 * a ^ 8 + 0.1815447799e7 * c ^ 6 * a ^ 6 - 0.5120907418e3 * c ^ 8 * a ^ 4 + 0.1885489314e1 * a ^ 2 * c ^ 10 - 0.8368566306e4 * c ^ 2 * a ^ 8 - 0.4108305749e5 * c ^ 4 * a ^ 6 + 0.6065237395e3 * c ^ 6 * a ^ 4 + 0.1220234699e5 * c ^ 2 * a ^ 6 - 0.1795926849e3 * c ^ 4 * a ^ 4 + 0.6608059271e0 * c ^ 6 * a ^ 2 - 0.2232435902e1 * c ^ 8 * a ^ 2;
end
clear
x_range = -1:0.01:1; % 定义 a 的初值范围
y_range = -1:0.05:1; % 定义 c 的初值范围
sol = []; % 用于存储唯一解的矩阵
options = optimoptions('fsolve', 'Display', 'off'); % 设置 fsolve 参数,不显示输出
% 遍历区间中的初值组合
for j = x_range
for i = y_range
x0 = [j, i]; % 当前初始点
try
% 尝试使用 fsolve 求解
[X_sol, fval, exitflag] = fsolve(@modulation_equation, x0, options);
if exitflag > 0 % 仅当成功求解时处理
% 检查新解是否已存在于 sol 中(判断相似解)
if isempty(sol) || all(vecnorm(sol - X_sol, 2, 2) > 1e-2)
sol = [sol; X_sol]; % 添加唯一解 (a, c) 作为一行
end
end
catch
% 如果 fsolve 失败,跳过该点
continue
end
end
end
sol = unique(sol, 'rows', 'stable');
sol1= sol(sol(:, 1) ~= 0 & sol(:, 2) ~= 0, :);
sol2=sqrt(sol1(:, 1).^2 + sol1(:, 2).^2);
sol3=sort(unique(sol2,'rows', 'stable'))
  댓글 수: 1
Alex Sha
Alex Sha 2024년 11월 18일
some thing wrong? the exact solution is: a=0, c=0

댓글을 달려면 로그인하십시오.

채택된 답변

Naga
Naga 2024년 11월 18일
To improve fsolve results, adjust the initial guess range (x_range and y_range) and step size to focus on promising regions. Modify tolerance settings using optimoptions to filter closely spaced solutions. After obtaining solutions, refine them with an appropriate distance threshold, adjusting from 1e-2 as needed for distinctness. Please go through the updated code for your reference:
function [F] = modulation_equation(X)
a = X(1);
c = X(2);
F(1) = 0.1096622461 * a^6 - 0.04223705634 * a^8 + 0.004066962405 * a^10 ...
- 0.001613995833 * c^2 * a^4 + 0.00005938649525 * c^4 * a^2 ...
- 31638471.88 * c^2 * a^8 + 868923.4851 * c^4 * a^6 ...
+ 970.5023202 * c^6 * a^4 - 0.8469105945 * c^2 * a^6 ...
+ 0.0120301483 * c^4 * a^4 - 0.00004506937871 * c^6 * a^2 ...
+ 80.83313632 * c^8 * a^2;
F(2) = -0.6970477491e12 * a^12 + 0.1914345842e11 * c^2 * a^10 ...
+ 0.2138367102e9 * c^4 * a^8 + 0.1815447799e7 * c^6 * a^6 ...
- 5120.907418 * c^8 * a^4 + 1.885489314 * a^2 * c^10 ...
- 83685.66306 * c^2 * a^8 - 410830.5749 * c^4 * a^6 ...
+ 6065.237395 * c^6 * a^4 + 122023.4699 * c^2 * a^6 ...
- 1795.926849 * c^4 * a^4 + 0.6608059271 * c^6 * a^2 ...
- 2.232435902 * c^8 * a^2;
end
clear
x_range = -1:0.05:1; % Adjusted step size for a
y_range = -1:0.1:1; % Adjusted step size for c
sol = []; % To store unique solutions
options = optimoptions('fsolve', 'Display', 'off', 'TolFun', 1e-6, 'TolX', 1e-6);
% Iterate over initial guesses
for j = x_range
for i = y_range
x0 = [j, i];
try
[X_sol, ~, exitflag] = fsolve(@modulation_equation, x0, options);
if exitflag > 0
if isempty(sol) || all(vecnorm(sol - X_sol, 2, 2) > 1e-2)
sol = [sol; X_sol];
end
end
catch
continue
end
end
end
% Ensure unique solutions
sol = unique(sol, 'rows', 'stable');
sol1 = sol(sol(:, 1) ~= 0 & sol(:, 2) ~= 0, :);
sol2 = sqrt(sol1(:, 1).^2 + sol1(:, 2).^2);
sol3 = sort(unique(sol2));

추가 답변 (1개)

Matt J
Matt J 2024년 11월 18일
given too many results
Perhaps you should use uniquetol, rather than unique.
  댓글 수: 2
fayu
fayu 2024년 11월 18일
Thank you for your suggestion, but using uniquetol command still can not solve
Matt J
Matt J 2024년 11월 18일
We don't know what that means. You haven't shown your attempt to use uniquetol() and the outcome.

댓글을 달려면 로그인하십시오.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by