How to use secant method to solve two equations
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I would like to solve these two equations (with the help of using anonymous functions) using the secant method.
460cos(theta)*t + 210.6*t-25082 = 0
460sin(theta)*t -6248 = 0
I am trying to solve for theta and t.
0 Commenti
Risposte (2)
MICHAEL MUTWIRI
il 23 Giu 2021
clc;clear;close all
% define the function as anonymous
f = @(x,t) [460.*cos(x).*t+210.6.*t-25082;460.*sin(x).*t-6248]; % x = theta
theta_0 =0;% Initial guess for Theta
t_0 = 1; % Inital guess for t
es = 1e-1; % percentage tolerance es = (new-old)/new * 100%
maxit = 60; % Maximum number of iterations
dh = 1e-3; % step size to used in finite differences method to compute the
% Jacobian
Initial_guess = [theta_0; t_0]; % column vector
[x,f,ea,iter]=secantNon_linear(f,Initial_guess,es,maxit,dh);
your_function_Solution=x
function_at_the_found_solution = f
disp('Solution does not converge')
%% TEST THE CODE USING A FUNCTION WITH KNOW SOLUTION
disp(' ')
disp(' ')
disp(' ')
disp('*****************************************************************')
disp('Testing SECANT method with functions with known solution')
disp('fsolve built-in function is used to confirm the solution')
disp('of the test function')
fprintf('Test functions\n')
fprintf(' : f1 = x1.^2+x2.^2-5\n')
fprintf(' : f2 = x2+1-x1.^2\n')
% Here is a test for the code to just confirm the code works
func = @(x1,x2) [x1.^2+x2.^2-5;x2+1-x1.^2];
x0 = [1.2;1.2];
es = 5;
dh = 1e-3;
maxit = 50;
[x,f,ea,iter] = secantNon_linear(func,x0,es,maxit,dh);
secant__TEST_solution = x'
% define the function again in way fsolve can use it: x1 = t(1); x2 = t(2)
func_for_fsolve = @(t) [t(1).^2+t(2).^2-5;t(2)+1-t(1).^2];
fsolve_CONFIRM_solution = fsolve(func_for_fsolve,x0')
%% THE SECANT CODE
function [x,f,ea,iter]=secantNon_linear(fun_xy,Initial_guess,tolerance,maxit,dh)
iter = 0;
x=Initial_guess;
while (1)
f=fun_xy(x(1),x(2));
% Jacobian matrix computed using the finite differences method
dfdx = (fun_xy(x(1)+dh,x(2))-fun_xy(x(1),x(2)))./dh;
dfdy = (fun_xy(x(1),x(2)+dh)-fun_xy(x(1),x(2)))./dh;
J = [dfdx dfdy];
dx=J\f;
x=x-dx;
iter = iter + 1;
ea=100*max(abs(dx./x));
if iter>=maxit||ea<=tolerance, break, end
end
end
1 Commento
MICHAEL MUTWIRI
il 23 Giu 2021
for the specific question posted--the solution did not converge----check a test of the codes using a different function with knows solutions
John D'Errico
il 2 Dic 2018
The secant method does not have a simple extension into multiple dimensions, although I am sure one could cobble something up. Far better however is to simply use tools that ARE designed for multiple variables, such as Newton-Raphson. Better yet of course, is to NOT write your own code to solve nonlinear equations. Never write your own numerical code to do something you do not fully understand, especially when professionally written code is available. (And if you do want to use the secant method here, then it is also clear you do not indeed fully understand the issues.)
Instead, use fsolve (from the optimization toolbox) or solve/vpasolve (from the symbolic toolbox), or lacking those TBs, you could even use fminsearch.
And if you cannot figure out how to implement it with fminsearch, then it is trivially simple to just solve the second equation for t, then substitute into the first equation.
t = 6248/(460*sin(theta))
When you eliminate t in the first equation, you now have a simple equation, solvable using fzero. That would find a solution at theta around 0.35502 (radians). Then just recover the value of t. Of course, there are infinitely may solutions.
0 Commenti
Vedere anche
Categorie
Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!