Display answer in degree

4 次查看(过去 30 天)
Ali Ahmed
Ali Ahmed 2023-12-29
评论: Dyuman Joshi 2023-12-30
Hello my friends,
I am trying solve 4 eqution using the following code
% Define the system of nonlinear equations
equations = @(x) [cos(7*x(1)) + cos(7*x(2)) + cos(7*x(3)) + cos(7*x(4));
cos(11*x(1)) + cos(11*x(2)) + cos(11*x(3)) + cos(11*x(4));
cos(13*x(1)) + cos(13*x(2)) + cos(13*x(3)) + cos(13*x(4));
cos(x(1)) + cos(x(2)) + cos(x(3)) + cos(x(4)) - 3.2];
% Initial guess for the solution (in radians)
initialGuess = [0; 0; 0; 0];
% Solve the system using fsolve
options = optimoptions('fsolve', 'Display', 'off'); % Suppress fsolve display
solution = fsolve(equations, initialGuess, options);
% Display the result
disp('Solution (in radians):');
disp(solution);
how to display the answer in degree
not I am new to matlab and find this code through code generator

回答(2 个)

Walter Roberson
Walter Roberson 2023-12-29
disp('Solution (in degrees):');
disp(rad2deg(solution));

Torsten
Torsten 2023-12-30
移动:Torsten 2023-12-30
"fsolve" is not able to find a solution for your system: the exitflag is -2.
% Define the system of nonlinear equations
equations = @(x) [cos(7*x(1)) + cos(7*x(2)) + cos(7*x(3)) + cos(7*x(4));
cos(11*x(1)) + cos(11*x(2)) + cos(11*x(3)) + cos(11*x(4));
cos(13*x(1)) + cos(13*x(2)) + cos(13*x(3)) + cos(13*x(4));
cos(x(1)) + cos(x(2)) + cos(x(3)) + cos(x(4)) - 3.2];
% Initial guess for the solution (in radians)
initialGuess = [0; 0; 0; 0];
% Solve the system using fsolve
options = optimoptions('fsolve', 'Display', 'off','MaxIterations',10000,'MaxFunctionEvaluations',10000); % Suppress fsolve display
[solution,fval,exitflag] = fsolve(equations, initialGuess, options)
solution = 4×1
0.5655 -0.1262 1.2984 -0.8435
fval = 4×1
-0.0651 0.0450 -0.0062 -0.4298
exitflag = -2
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-30
There is (atleast) a solution -
syms x [1 4]
eqs = [cos(7*x(1)) + cos(7*x(2)) + cos(7*x(3)) + cos(7*x(4));
cos(11*x(1)) + cos(11*x(2)) + cos(11*x(3)) + cos(11*x(4));
cos(13*x(1)) + cos(13*x(2)) + cos(13*x(3)) + cos(13*x(4));
cos(x(1)) + cos(x(2)) + cos(x(3)) + cos(x(4)) - 3.2]
eqs = 
%solve returns the output via vpasolve()
sol = vpasolve(eqs,x)
sol = struct with fields:
x1: -38.235216532077723407827330267693 x2: 288.35776383136872785096437139067 x3: 21544.805212430975000143326690952 x4: -30767.810938238740147856771781125
sol = struct2array(sol);
subs(eqs, x, sol)
ans = 
However, the values are quite scattered, maybe using a better initial point might result in a solution via fsolve().

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by