You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
solution using newton raphson method?
3 views (last 30 days)
Show older comments
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)
Accepted Answer
Ameer Hamza
on 19 Apr 2020
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 Comments
omkari sai krishna
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
Yes sir thank you i am able to get solution with your help.
Ameer Hamza
on 20 Apr 2020
I am glad to be of help.
omkari sai krishna
on 20 Apr 2020
Edited: Ameer Hamza
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
Edited: Ameer Hamza
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
sir i am comparing each value in column matrix k with 0.001
Ameer Hamza
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
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
on 20 Apr 2020
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
on 3 May 2020
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
on 3 May 2020
Edited: Ameer Hamza
on 3 May 2020
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
on 3 May 2020
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)