How can i solve following problems?

2 vues (au cours des 30 derniers jours)
PULAK Kumer
PULAK Kumer le 26 Déc 2020
Commenté : Walter Roberson le 28 Déc 2020
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 26 Déc 2020
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 commentaires
PULAK Kumer
PULAK Kumer le 28 Déc 2020
How can i use matlab function instead of inline here?
Walter Roberson
Walter Roberson le 28 Déc 2020
Change
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
to
f = matlabFunction(a, 'vars', x);
d = matlabFunction(diff(a), 'vars', x);

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by