solution using newton raphson method?
1 次查看(过去 30 天)
显示 更早的评论
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
2020-4-19
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
omkari sai krishna
2020-5-3
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)