this is my code i am unable to substitue x1,x2,x3,x4 values in the matrix.Please help in solve this.
syms x1 x2 x3 x4
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];
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')
disp(x)
disp(p)
subs(p)

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 19일

0 개 추천

MATLAB does not automatically know which values to substitute. You need to specify this in the call to subs() explicitly. Try the following code
syms x1 x2 x3 x4
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];
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');
disp(x)
disp(p)
subs(p, [x1 x2 x3 x4], [X1 X2 X3 X4])

댓글 수: 19

omkari sai krishna
omkari sai krishna 2020년 4월 20일
Thank you sir for your suggestion.
it is showing
Error using symengine
Division by zero.
Error in sym/subs>mupadsubs (line 160)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in sai (line 19)
subs(p, [x1 x2 x3 x4], [90 60 60 60])
omkari, the matrix p is quite complex and has several terms in the denominators of each element too. For some of the values of [x1 x2 x3 x4], the denominator becomes zero, and it gives the error about division by zero. For example, you can try this
subs(p, [x1 x2 x3 x4], [1 4 3 2])
and it will not give an error. You will need to see your model and find out which values are not allowed.
omkari sai krishna
omkari sai krishna 2020년 4월 20일
yes sir, thank you it is not showing error i had changed the initial values.
But i am not able to convert my p matrix in to a value althought i am using 'double' syntax.
It is showing unable to converter your expression in to double array.
please help me why it is showing that error.
You should call double like this
syms x1 x2 x3 x4
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];
j=jacobian(g,x);
j_inv=inv(j);
p=j_inv*-g;
double(subs(p, [x1 x2 x3 x4], [1 4 3 2]))
omkari sai krishna
omkari sai krishna 2020년 4월 20일
Yes sir thank you i am able to get solution with your help.
Ameer Hamza
Ameer Hamza 2020년 4월 20일
I am glad to be of help.
omkari sai krishna
omkari sai krishna 2020년 4월 20일
편집: Ameer Hamza 2020년 4월 20일
Sir one more doubt in this code the values in k matrix is not changing eventhough X matrix values are changing in each itteration.
This is my code
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)<10e-2
disp(X)
break
else
y1=X(1,1)+k(1,1);
y2=X(2,1)+k(2,1);
y3=X(3,1)+k(3,1);
y4=X(4,1)+k(4,1);
X(1,1)=y1;
X(2,1)=y2;
X(3,1)=y3;
X(4,1)=y4;
disp(X)
end
end
For this code k is not substituting the updated values of X matrix.
You need to use same variable names for substitution
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)<10e-2
disp(X)
break
else
y1=X(1,1)+k(1,1);
y2=X(2,1)+k(2,1);
y3=X(3,1)+k(3,1);
y4=X(4,1)+k(4,1);
X1=y1;
X2=y2;
X3=y3;
X4=y4;
disp(X)
end
end
if you want to save the values of each iteration, the do something like this
count = 1;
while (1)
k=double(subs(p, [x1 x2 x3 x4], [X1(count) X2(count) X3(count) X4(count)]));
count = count + 1;
if k(1,1)&&k(2,1)&&k(3,1)&&k(4,1)<10e-2
disp(X)
break
else
y1=X(1,1)+k(1,1);
y2=X(2,1)+k(2,1);
y3=X(3,1)+k(3,1);
y4=X(4,1)+k(4,1);
X1(count)=y1;
X2(count)=y2;
X3(count)=y3;
X4(count)=y4;
disp(X)
end
end
omkari sai krishna
omkari sai krishna 2020년 4월 20일
편집: Ameer Hamza 2020년 4월 20일
Sir i am working with your suggestion but i am not getting correct solution using this code i am not understanding what will be reason. Please help me to know the concept.
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
results are showing wrong values please find a solution
What are you trying to do in this condition?
k(1,1)&&k(2,1)&&k(3,1)&&k(4,1)<0.000001
omkari sai krishna
omkari sai krishna 2020년 4월 20일
sir i am comparing each value in column matrix k with 0.001
The correct condition is
if (k(1,1)<0.000001)&&(k(2,1)<0.000001)&&(k(3,1)<0.000001)&&(k(4,1)<0.000001)
or in a compact form
if all([k(1,1) k(2,1) k(3,1) k(4,1)]<0.000001)
However, note that this condition become true with all variable become negative, e.g., -1000 < 0.000001 is also true.
omkari sai krishna
omkari sai krishna 2020년 4월 20일
yes sir and it is not showing exact results loop is executing continously it is not showing any results.
sir is my equation is wrong? ?
I don't know the details of this method, so I cannot say if it is correct. But if you want to display values for all the iterations, then add these lines
disp(X1)
disp(X2)
disp(X3)
disp(X4)
disp(k)
inside the else block too. Right now, they are only displayed when the condition of 'if' block become true.
omkari sai krishna
omkari sai krishna 2020년 4월 20일
i am solving a set of non linear equations using newton raphson method using code.
i am not getting exact solution.
yes sir i had used in my code.
thank you sir its a great help for me .thank you so much.
omkari sai krishna
omkari sai krishna 2020년 5월 3일
sir i want one more help can u help me to get the code for the THD equation which i am placed below.
i had tried myself but it is always showing 1 as a answer. please help me to solve this.
Ameer Hamza
Ameer Hamza 2020년 5월 3일
편집: Ameer Hamza 2020년 5월 3일
Can you start a new question and post the details of your question? You can then post the link of your question in the next comment.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Community Treasure Hunt

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

Start Hunting!

Translated by