solving nonlinear equation using newton method
Réponses (2)
0 votes
I revamped the software code scientific computing TCE programme, here is good answer I provided:
clc
clear all
close all
function [v1 , no_itr, norm1] = NewtonRaphson_nl(v,fn,jacob_fn,no_itr,err)
% nargin = no. of input arguments
if nargin <5 , no_itr = 20 ; end
if nargin <4 , error = 10^-5;no_itr = 20 ; end
if nargin <3 ,no_itr = 20;error = 10^-5; v = [1;1;1]; end
v1 = v;
fnv1 = fn(v1);
i = 0;
while true
jacob_fnv1 = jacob_fn(v1);
H = jacob_fnv1\(fnv1+eps);
v1 = v1 - H;
fnv1 = fn(v1);
i = i + 1;
norm1 = norm(fnv1);
if i > no_itr || norm1 < err
break
end
%if norm(fnv1) < error , break , end
end
end
function [v1 , no_itr, norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,err)
v1 = v;
fnv1 = fn(v1);
i = 0;
fprintf(' Iteration| x | y | Error | \n')
while true
norm1 = norm(fnv1);
fprintf('%10d |%10.4f| %10.4f | %10.4d | \n',i,v1(1),v1(2),norm1)
jacob_fnv1 = jacob_fn(v1);
H = jacob_fnv1\(fnv1+eps);
v1 = v1 - H;
fnv1 = fn(v1);
i = i + 1;
norm1 = norm(fnv1);
if i > no_itr || norm1 < err
break
end
%if norm(fnv1) < error , break , end
end
end
%Function NewtonRaphson_nl() is given below.
%For your provided input:
% f(x1,y1)4-x^2-y^2=0
% f(x2,y2)1-e^x-y=0
%I corrected for you:
fn = @(v)[(v(1)^2+v(2)^2)/4;v(2) + exp(v(1))];
jacob_fn = @(v)[v(1) v(2);exp(v(1)) 1];
err = 10^-7;
v = [1 ;-1.7];
no_itr = 25;
[point,no_itr,error_out]=NewtonRaphson_nl(v,fn,jacob_fn,no_itr,err)
[v1,no_itr,norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,err)
%x = -0.1:0.01:0.1;
%y = x;
syms x y
[xsol,ysol] = vpasolve(fn([x,y]) == 0,[x,y],[1 1])
f12 = fn([x,y])
fimplicit(f12(1),'r')
hold on
fimplicit(f12(2),'b')
xlabel X
ylabel Y
hold off
grid on
axis equal
title('1st function')
%For 2nd function example:
fn = @(v) [-v(1)^2-v(2)^2+4 ; 1-exp(v(1))-v(2)];
jacob_fn = @(v) [-2*v(1),-2*v(2);-exp(v(1)),-1];
[point,no_itr,error_out]=NewtonRaphson_nl(v,fn,jacob_fn,no_itr,err)
[v1,no_itr,norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,err)
syms x y
[xsol,ysol] = vpasolve(fn([x,y]) == 0,[x,y],[1 1])
f22 = fn([x,y])
fimplicit(f22(1),'r')
hold on
fimplicit(f22(2),'b')
xlabel X
ylabel Y
hold off
grid on
axis equal
title('2nd function')
fn = @(v)[2*v(1)^5+4;v(2) + exp(v(2))*exp(v(1))];
jacob_fn = @(v) [10*v(1)^4,0;exp(v(1))*exp(v(2)),exp(v(1))*exp(v(2))+1];
[point,no_itr,error_out]=NewtonRaphson_nl(v,fn,jacob_fn,no_itr,err)
[v1,no_itr,norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,err)
syms x y
[xsol,ysol] = vpasolve(fn([x,y]) == 0,[x,y],[1 1])
f22 = fn([x,y])
fimplicit(f22(1),'r')
hold on
fimplicit(f22(2),'b')
xlabel X
ylabel Y
hold off
grid on
axis equal
title('3rd function')
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m
2 commentaires
- I have added recently solution with clear root.
Catégories
En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
