solution using newton raphson method?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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]))
Yes sir thank you i am able to get solution with your help.
Ameer Hamza
2020년 4월 20일
I am glad to be of help.
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
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
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.
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.
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.
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
참고 항목
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)
