Clear Filters
Clear Filters

Display answer in degree

10 views (last 30 days)
Ali Ahmed
Ali Ahmed on 29 Dec 2023
Commented: Dyuman Joshi on 30 Dec 2023
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

Answers (2)

Walter Roberson
Walter Roberson on 29 Dec 2023
disp('Solution (in degrees):');
disp(rad2deg(solution));

Torsten
Torsten on 30 Dec 2023
Moved: Torsten on 30 Dec 2023
"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 Comment
Dyuman Joshi
Dyuman Joshi on 30 Dec 2023
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().

Sign in to comment.

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!