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])
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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.
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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.
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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? ?
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
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