Newton method for solving a nonlinear system of equations!
Hello,
I am trying to write a function that solves a non linear system of equations. In my exercise I have 2,3 and 4 dimensional equation system. I am not allowed to use fsolve function. I also have a function called "Score4" which is suppose to calculate the score of my function ( should be around 1 if the code that I wrote is correct).
Below you will find the score and the code that I wrote.
Thank you in advance for your time.
function [score,error,details] = Score4()
%% Parameter
% 2 dimensional equation system
A{1} = @(x)[4*cos(x(1)),2*cos(x(1)+x(2));
5*sin(x(1)),1*sin(x(1)+x(2))];
x0{1} = [0,0]’;
y{1} = [2,3]’;
y0{1} = [-1,1]’;
% 3 dimensional equation system
A{2} = @(x)[1,0,0;0,cos(x(1)),-sin(x(2));0,sin(x(2))^2,cos(x(1))];
x0{2} = [0;0];
y0{2} = [1;2;3];
y{2} = [1;0;1];
% 4 dimensional transformation matrix with 5d input
A{3} = @(x)[cos(x(1)),sin(x(1)),0,-x(3);
-sin(x(1))*cos(x(4)),cos(x(1))*cos(x(4)),sin(x(4))*sin(x(5)),-x(2)*sin(x(4))*cos(x(5));
sin(x(4))*sin(x(1)),-cos(x(1))*sin(x(4)),cos(x(4))*sin(x(5)),-x(2)*cos(x(4))*cos(x(5));
0,0,0,1];
x0{3} = [0;0;0;0;0];
y0{3} = [1;3;2;1];
y{3} = [1;2;-1;1];
%% Score Berechnung
score = 0;
error = ”;
details = struct();
try
% Iteration ueber testcases
for i = 1:3
% Aufstellen des nullstellenproblems
RootProblem = @(z) A{i}(z)*y0{i}-y{i};
T = 0;
% Mitteln ueber Zeit
timer = tic();
for j = 1:10
x = nlinSolve(RootProblem,x0{i});
end
T = toc(timer)/10;
output = A{i}(x)*y0{i}-y{i};
Err = norm(output);
Sc = (Err+0.01)*(T+0.5);
score = score+Sc;
details.([‘time’,num2str(i)]) = T;
details.([‘error’,num2str(i)]) = Err;
end
score = 0.017 / score;
catch err
score = 0;
error = err.message;
end
end
To solve this problem I was thinking to use Newton method but I am having a lot of problems. Can someone give me some suggestions how to improve my code and what am I doing wrong. My code is as follows:
function x = nlinSolve (g,x0)
select = @(g,r,c) g(r,c);
G1 = @(x)select(g(x), 1, 1); % selecting the first row of g(x)
G2 = @(x)select(g(x), 2, 1); % selecting the second row
G3 = @(x)select(g(x), 3, 1);
G4 = @(x)select(g(x), 4, 1);
G5 = @(x)select(g(x), 5, 1);
x = x0; % x0 initial guess which is given in the score function
for m=1:10
%calculating Jacobian
for k=1:numel(x)
for n =1: numel(x)
Jakobian(k,n)= Derivative(eval([‘G’,num2str(k)]),x,x(n),n);
end
end
x = x-inv(Jakobian)*g(x); % Newton formula to find roots
end
end
function J = Derivative(G,x,xm,m)
% the funciton that calculates the derivative using numerical method
e = 0.0001;
x1=x;
x2=x;
x1(m) = xm;
x2(m) = xm+e;
J = (G(x2)-G(x1))/e;
end
Now this code is clearly not working, I am messing up something. Also the score should be around 1 at the end.
If anyone can help me that would be more than appritiated.Hello,
I am trying to write a function that solves a non linear system of equations. In my exercise I have 2,3 and 4 dimensional equation system. I am not allowed to use fsolve function. I also have a function called "Score4" which is suppose to calculate the score of my function ( should be around 1 if the code that I wrote is correct).
Below you will find the score and the code that I wrote.
Thank you in advance for your time.
function [score,error,details] = Score4()
%% Parameter
% 2 dimensional equation system
A{1} = @(x)[4*cos(x(1)),2*cos(x(1)+x(2));
5*sin(x(1)),1*sin(x(1)+x(2))];
x0{1} = [0,0]’;
y{1} = [2,3]’;
y0{1} = [-1,1]’;
% 3 dimensional equation system
A{2} = @(x)[1,0,0;0,cos(x(1)),-sin(x(2));0,sin(x(2))^2,cos(x(1))];
x0{2} = [0;0];
y0{2} = [1;2;3];
y{2} = [1;0;1];
% 4 dimensional transformation matrix with 5d input
A{3} = @(x)[cos(x(1)),sin(x(1)),0,-x(3);
-sin(x(1))*cos(x(4)),cos(x(1))*cos(x(4)),sin(x(4))*sin(x(5)),-x(2)*sin(x(4))*cos(x(5));
sin(x(4))*sin(x(1)),-cos(x(1))*sin(x(4)),cos(x(4))*sin(x(5)),-x(2)*cos(x(4))*cos(x(5));
0,0,0,1];
x0{3} = [0;0;0;0;0];
y0{3} = [1;3;2;1];
y{3} = [1;2;-1;1];
%% Score Berechnung
score = 0;
error = ”;
details = struct();
try
% Iteration ueber testcases
for i = 1:3
% Aufstellen des nullstellenproblems
RootProblem = @(z) A{i}(z)*y0{i}-y{i};
T = 0;
% Mitteln ueber Zeit
timer = tic();
for j = 1:10
x = nlinSolve(RootProblem,x0{i});
end
T = toc(timer)/10;
output = A{i}(x)*y0{i}-y{i};
Err = norm(output);
Sc = (Err+0.01)*(T+0.5);
score = score+Sc;
details.([‘time’,num2str(i)]) = T;
details.([‘error’,num2str(i)]) = Err;
end
score = 0.017 / score;
catch err
score = 0;
error = err.message;
end
end
To solve this problem I was thinking to use Newton method but I am having a lot of problems. Can someone give me some suggestions how to improve my code and what am I doing wrong. My code is as follows:
function x = nlinSolve (g,x0)
select = @(g,r,c) g(r,c);
G1 = @(x)select(g(x), 1, 1); % selecting the first row of g(x)
G2 = @(x)select(g(x), 2, 1); % selecting the second row
G3 = @(x)select(g(x), 3, 1);
G4 = @(x)select(g(x), 4, 1);
G5 = @(x)select(g(x), 5, 1);
x = x0; % x0 initial guess which is given in the score function
for m=1:10
%calculating Jacobian
for k=1:numel(x)
for n =1: numel(x)
Jakobian(k,n)= Derivative(eval([‘G’,num2str(k)]),x,x(n),n);
end
end
x = x-inv(Jakobian)*g(x); % Newton formula to find roots
end
end
function J = Derivative(G,x,xm,m)
% the funciton that calculates the derivative using numerical method
e = 0.0001;
x1=x;
x2=x;
x1(m) = xm;
x2(m) = xm+e;
J = (G(x2)-G(x1))/e;
end
Now this code is clearly not working, I am messing up something. Also the score should be around 1 at the end.
If anyone can help me that would be more than appritiated. Hello,
I am trying to write a function that solves a non linear system of equations. In my exercise I have 2,3 and 4 dimensional equation system. I am not allowed to use fsolve function. I also have a function called "Score4" which is suppose to calculate the score of my function ( should be around 1 if the code that I wrote is correct).
Below you will find the score and the code that I wrote.
Thank you in advance for your time.
function [score,error,details] = Score4()
%% Parameter
% 2 dimensional equation system
A{1} = @(x)[4*cos(x(1)),2*cos(x(1)+x(2));
5*sin(x(1)),1*sin(x(1)+x(2))];
x0{1} = [0,0]’;
y{1} = [2,3]’;
y0{1} = [-1,1]’;
% 3 dimensional equation system
A{2} = @(x)[1,0,0;0,cos(x(1)),-sin(x(2));0,sin(x(2))^2,cos(x(1))];
x0{2} = [0;0];
y0{2} = [1;2;3];
y{2} = [1;0;1];
% 4 dimensional transformation matrix with 5d input
A{3} = @(x)[cos(x(1)),sin(x(1)),0,-x(3);
-sin(x(1))*cos(x(4)),cos(x(1))*cos(x(4)),sin(x(4))*sin(x(5)),-x(2)*sin(x(4))*cos(x(5));
sin(x(4))*sin(x(1)),-cos(x(1))*sin(x(4)),cos(x(4))*sin(x(5)),-x(2)*cos(x(4))*cos(x(5));
0,0,0,1];
x0{3} = [0;0;0;0;0];
y0{3} = [1;3;2;1];
y{3} = [1;2;-1;1];
%% Score Berechnung
score = 0;
error = ”;
details = struct();
try
% Iteration ueber testcases
for i = 1:3
% Aufstellen des nullstellenproblems
RootProblem = @(z) A{i}(z)*y0{i}-y{i};
T = 0;
% Mitteln ueber Zeit
timer = tic();
for j = 1:10
x = nlinSolve(RootProblem,x0{i});
end
T = toc(timer)/10;
output = A{i}(x)*y0{i}-y{i};
Err = norm(output);
Sc = (Err+0.01)*(T+0.5);
score = score+Sc;
details.([‘time’,num2str(i)]) = T;
details.([‘error’,num2str(i)]) = Err;
end
score = 0.017 / score;
catch err
score = 0;
error = err.message;
end
end
To solve this problem I was thinking to use Newton method but I am having a lot of problems. Can someone give me some suggestions how to improve my code and what am I doing wrong. My code is as follows:
function x = nlinSolve (g,x0)
select = @(g,r,c) g(r,c);
G1 = @(x)select(g(x), 1, 1); % selecting the first row of g(x)
G2 = @(x)select(g(x), 2, 1); % selecting the second row
G3 = @(x)select(g(x), 3, 1);
G4 = @(x)select(g(x), 4, 1);
G5 = @(x)select(g(x), 5, 1);
x = x0; % x0 initial guess which is given in the score function
for m=1:10
%calculating Jacobian
for k=1:numel(x)
for n =1: numel(x)
Jakobian(k,n)= Derivative(eval([‘G’,num2str(k)]),x,x(n),n);
end
end
x = x-inv(Jakobian)*g(x); % Newton formula to find roots
end
end
function J = Derivative(G,x,xm,m)
% the funciton that calculates the derivative using numerical method
e = 0.0001;
x1=x;
x2=x;
x1(m) = xm;
x2(m) = xm+e;
J = (G(x2)-G(x1))/e;
end
Now this code is clearly not working, I am messing up something. Also the score should be around 1 at the end.
If anyone can help me that would be more than appritiated. newton method, nonlinear system of equations, matlab MATLAB Answers — New Questions