How can i solve following problems?

The code is :
% Program Code of finding root in MATLAB created by Pulak
clc,clear all
syms x
a=input('Enter the function in the form of variable x:');
err=10;
disp('Do you have initial approximate value in your math?')
b=input('If yes ,Press 1,If no press 2: ');
if (b~=1)
x(1)=1;
else
x(1)=input('Enter Initial Guess:');
end
n=input('Enter decimal place');
tol=1/(10^(n-1))
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for k=2:1000
x(k)=x(k-1)-((f(x(k-1))/d(x(k-1))));
err=abs((x(k)-x(k-1))/x(k));
if err<tol
break
end
end
k
fprintf('%.*f',n,x(k))
but after run this code, I see:
--------------------------------------
Enter the function in the form of variable x:x^2+(4*sin(x))
Do you have initial approximate value in your math?
If yes ,Press 1,If no press 2: 2
Enter decimal place4
tol =
1.0000e-03
f =
Inline function:
f(x) = sin(x).*4.0+x.^2
Conversion to logical from sym is not possible.
Error in final_newton_raphson (line 22)
if err<tol
how can i solve this ? & I also can not give input a complex value for x(1) and i cannot also get complex root .How can i solve this sir?

 Accepted Answer

if (b~=1)
x(1)=1;
else
x(1)=input('Enter Initial Guess:');
end
Change that to
if (b~=1)
x0=1;
else
x0=input('Enter Initial Guess:');
end
and before
for k=2:1000
insert
x = x0;
I also can not give input a complex value for x(1)
Just enter it at the prompt
>> x0 = input('Enter Initial Guess: ')
Enter Initial Guess: 3+5i
x0 =
3 + 5i

11 Comments

We recommend against using inline(). It has been recommended against since function handles were introduced in MATLAB 6.3, close to 20 years go.
You are using the Symbolic Toolbox. Use matlabFunction() instead of inline.
After running correcting the previous code, i got:
Enter the function in the form of variable x:
x^4+x^3+5*x^2+4*x+4
Do you have initial approximate value in your math?
If yes ,Press 1,If no press 2:
1
Enter Initial Guess:
i
Enter decimal place
5
tol =
1.0000e-04
f =
Inline function:
f(x) = x.*4.0+x.^2.*5.0+x.^3+x.^4+4.0
x =
0.0000 + 1.0000i
By using Bewton-Raphson Method, we get
The Root is: -0.50000
here output (root) came only real value , but it should be a complex numer.How can i fixed it sir?
fprintf() only displays the real portion of a value.
fprintf('%.*f %+.fi\n',n, real(x(k)), n, imag(x(k)));
By using this..i cannot get the acctual value for the imaginary part
% Program Code of finding root in MATLAB created by Pulak
syms x
disp('Enter the function in the form of variable x: ');
Enter the function in the form of variable x:
%{
a=input();
%}
a = x^4+x^3+5*x^2+4*x+4 %FOR DEMO
a = 
err=10;
disp('Do you have initial approximate value in your math?')
Do you have initial approximate value in your math?
disp('If yes ,Press 1,If no press 2: ')
If yes ,Press 1,If no press 2:
%{
b=input();
%}
b = 1 %FOR DEMO
b = 1
if (b~=1)
x0=1;
else
disp('Enter Initial Guess: ')
%{
x0=input();
%}
x0 = 1i %FOR DEMO
end
Enter Initial Guess:
x0 = 0.0000 + 1.0000i
disp('Enter decimal place ')
Enter decimal place
%{
n=input();
%}
n = 5 %FOR DEMO
n = 5
tol=1/(10^(n-1))
tol = 1.0000e-04
f=inline(a)
f = Inline function: f(x) = x.*4.0+x.^2.*5.0+x.^3+x.^4+4.0
dif=diff(sym(a));
d=inline(dif);
x = x0;
for k=2:1000
x(k)=x(k-1)-((f(x(k-1))/d(x(k-1))));
err=abs((x(k)-x(k-1))/x(k));
if err<tol
break
end
end
k
k = 5
fprintf('%.*f %+.*fi\n',n, real(x(k)), n, imag(x(k)));
-0.50000 +0.86603i
When we run the following code:
% Program Code of Newton-Raphson Method in MATLAB
clc,clear all
fprintf('********<strong>Code has been established by Pulak Kundu(181201)</strong>********\n')
fprintf('********<strong>Code for imaginary root using Newton-Rapshon Method</strong>********\n')
syms x
a=input('Enter the function in the form of variable x:');
err=10;
disp('Do you have initial approximate value in your math?')
b=input('If yes ,Press 1,If no press 2: ');
if (b~=1)
x0=1;
else
x0=input('Enter Initial Approximate value:');
end
n=input('Enter decimal place:');
tol=1/(10^(n-1));
disp("Your inputed equation is as polynomial:")
a
f=inline(a);
dif=diff(sym(a));
d=inline(dif);
x=x0;
for k=2:1000
x(k)=x(k-1)-((f(x(k-1))/d(x(k-1))));
err=abs((x(k)-x(k-1))/x(k));
if err<tol
break
end
end
fprintf('<strong>By using Bewton-Raphson Method, we get</strong>')
fprintf('\n\t\t\t\t<strong>The Root is: %.*f + %.*fi\n</strong>',n,real(x(k)),n,imag(x(k)));
fprintf('********<strong>Code has been established by Pulak Kundu(181201)</strong>********\n')
We see the following output:
********Code has been established by Pulak Kundu(181201)********
********Code for imaginary root using Newton-Rapshon Method********
Enter the function in the form of variable x:
x^4-(5*x^3)+(20*x^2)-(40*x)+60
Do you have initial approximate value in your math?
If yes ,Press 1,If no press 2:
2
Enter decimal place:
4
Your inputed equation is as polynomial:
a =
x^4 - 5*x^3 + 20*x^2 - 40*x + 60
By using Bewton-Raphson Method, we get
The Root is: -9.5195 + 0.0000i
********Code has been established by Pulak Kundu(181201)********
but the real answer is 1.915+1.908 i.
What can i do now?
I will check later after I wake up
By the way: why are you using inline() ? I guarantee you that there are cases where using inline() will produce results that you do not expect. Why are you not using matlabFunction(), which is specifically designed to convert symbolic expressions into anonymous functions?
In your code, when the user indicates they do not have an initial value, you use a default initial value of 1 .
The Newton-Raphson method uses the ratio of the function and its derivative. Your given function always produces real values when given real input, and the difference of real values is always real, so we can see after a moment of reflection about the definition of derivative as a limit of slopes of differences, that over a section where a function produces real outputs for real inputs, that it follows that the derivative must also be real valued. You are then taking the ratio of two real values, and the result is going to be real-valued.
Therefore, when you use Newton-Raphson on a function that produces real outputs for real inputs, it follows that the Newton-Raphson method will only ever project to a new real-valued location and will never project to a complex-valued location. Therefore when your function produces real outputs for real inputs and you use a real-valued default input, the Newton-Raphson method can never find a complex root.
The function stops running because you reach the iteration limit, not because it reaches the error tolerance.
How can i use matlab function instead of inline here?
Change
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
to
f = matlabFunction(a, 'vars', x);
d = matlabFunction(diff(a), 'vars', x);

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!