sir, i am not able develop a matlab code to solve four transcendental equations using newton raphson method? i had tried till the creation of jacobian matrix but not able to substitute values in the jacobian matrix please help to solve this problem?

syms x1 x2 x3 x4 epsi;
g1=cos(x1)+cos(x2)+cos(x3)+cos(x4)-0.9424;
g2=cos(5*x1)+cos(5*x2)+cos(5*x3)+cos(5*x4);
g3=cos(7*x1)+cos(7*x2)+cos(7*x3)+cos(7*x4);
g4=cos(11*x1)+cos(11*x2)+cos(11*x3)+cos(11*x4);
g=[g1;g2;g3;g4];
x=[x1; x2; x3; x4];
epsi=10e-5;
j=jacobian(g,x);
for i=1:4
x(i,1)=input('enter the intial values\n')
end
disp(x)
f=subs(j);
j_inv=inv(f);
disp(-g);
k=subs(-g);
p=j_inv*k;
This is my code i am not able to substitute initial values in 'P' matrix. please help me to solve this.

 Accepted Answer

In the given code, the values are not substituted in the jacobian matrix because the initial values are assigned to the variable 'x' but the variables used in the equations are 'x1,x2,x3 and x4'. Please find the code below to substitute the initial values.
x1 = x(1,1);
x2 = x(2,1);
x3 = x(3,1);
x4 = x(4,1);
For more information, refer to this file exchange link

5 Comments

Thank you mam for your support. I need further help in this question. I had developed a matlab code to solve a set of nonlinear equations using newton raphson method. But the code which I had developed had not able to give solution. Can u please guide me in solving this.
clc;clear all syms x1 x2 x3 x4 X1 X2 X3 X4 g1=cosd(x1)+cosd(x2)+cosd(x3)+cosd(x4)-0.9424; g2=cosd(5*x1)+cosd(5*x2)+cosd(5*x3)+cosd(5*x4); g3=cosd(7*x1)+cosd(7*x2)+cosd(7*x3)+cosd(7*x4); g4=cosd(11*x1)+cosd(11*x2)+cosd(11*x3)+cosd(11*x4); g=[g1;g2;g3;g4]; x=[x1;x2;x3;x4]; j=jacobian(g,x); j_inv=inv(j); p=j_inv*-g; X1=input('enter initial values of x1'); X2=input('enter initial values of x2'); X3=input('enter initial values of x3'); X4=input('enter initial values of x4'); X=[X1;X2;X3;X4]; while (1) k=double(subs(p, [x1 x2 x3 x4],[X1 X2 X3 X4])); if k(1,1)&&k(2,1)&&k(3,1)&&k(4,1)<0.000001 disp(X1) disp(X2) disp(X3) disp(X4) disp(k) break else y1=X1+k(1,1); y2=X2+k(2,1); y3=X3+k(3,1); y4=X4+k(4,1); X1=y1; X2=y2; X3=y3; X4=y4;
end
end
Mam this is my code which I had explained in previous comment. I am getting solutions more than 90degress but i have to get all the values below 90degrees please help me to solve this problem.
I hope this link helps you in getting the correct output to solve a set of nonlinear equations using newton raphson method.
Thank you mam for your support but still i am not able to solve this problem. I am giving the updated code for four nonlinear equations please give me suggestion where i am doing wrong.
clc;
close all;
clear all;
syms x y z v;
f=cosd(x)+cosd(y)+cosd(z)+cosd(v)-1.884;
fx=diff(f,x);fy=diff(f,y);fz=diff(f,z);fv=diff(f,v);
g=cosd(5*x)+cosd(5*y)+cosd(5*z)+cosd(5*v);
gx=diff(g,x);gy=diff(g,y);gz=diff(g,z);gv=diff(g,v);
h=cosd(7*x)+cosd(7*y)+cosd(7*z)+cosd(7*v);
hx=diff(h,x);hy=diff(h,y);hz=diff(h,z);hv=diff(h,v);
l=cosd(11*x)+cosd(11*y)+cosd(11*z)+cosd(11*v);
lx=diff(l,x);ly=diff(l,y);lz=diff(l,z);lv=diff(l,v);
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the x intial approximation:');
y0 = input('Enter the y intial approximation:');
z0 = input('Enter the z intial approximation:');
v0 = input('Enter the v initial value:');
xk = x0;yk = y0;zk = z0;vk = v0;
for i=1:100
fk=vpa(subs(f,[x y z v],[xk yk zk vk]));
gk=vpa(subs(g,[x y z v],[xk yk zk vk]));
hk=vpa(subs(h,[x y z v],[xk yk zk vk]));
lk=vpa(subs(l,[x y z v],[xk yk zk vk]));
fxk=vpa(subs(fx,[x y z v],[xk yk zk vk]));
fyk=vpa(subs(fy,[x y z v],[xk yk zk vk]));
fzk=vpa(subs(fz,[x y z v],[xk yk zk vk]));
fvk=vpa(subs(fv,[x y z v],[xk yk zk vk]));
gxk=vpa(subs(gx,[x y z v],[xk yk zk vk]));
gyk=vpa(subs(gy,[x y z v],[xk yk zk vk]));
gzk=vpa(subs(gz,[x y z v],[xk yk zk vk]));
gvk=vpa(subs(gv,[x y z v],[xk yk zk vk]));
hxk=vpa(subs(hx,[x y z v],[xk yk zk vk]));
hyk=vpa(subs(hy,[x y z v],[xk yk zk vk]));
hzk=vpa(subs(hz,[x y z v],[xk yk zk vk]));
hvk=vpa(subs(hv,[x y z v],[xk yk zk vk]));
lxk=vpa(subs(lx,[x y z v],[xk yk zk vk]));
lyk=vpa(subs(ly,[x y z v],[xk yk zk vk]));
lzk=vpa(subs(lz,[x y z v],[xk yk zk vk]));
lvk=vpa(subs(lv,[x y z v],[xk yk zk vk]));
xyzvk = [xk yk zk vk]'; %The old values of x,y,z
J = [fxk fyk fzk fvk;gxk gyk gzk gvk;hxk hyk hzk hvk;lxk lyk lzk lvk];
Fk = [fk gk hk vk]'; % Matrix of function values
xyzvkplus1 = (xyzvk - inv(J)*Fk);
if abs(xyzvkplus1 - xyzvk) < epsilon
break;
else
display('no solution');
end
end
xyzvans = xyzvkplus1-rem(xyzvkplus1,10^-n);
fprintf('The Root Matrix is :\n');
display(xyzvans);
for this code i am getting soltion more than 90 degrees.
In the above code, you are computing the answer 'xyzvans' even if there is no solution available. Use the return command after display('no solution'); to stop running this script when there is no solution. In this way, the answer will be computed only when the error value is less than epsilon.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!