How can i solve following problems?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
PULAK Kumer
am 26 Dez. 2020
Kommentiert: Walter Roberson
am 28 Dez. 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?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Dez. 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 Kommentare
Walter Roberson
am 28 Dez. 2020
Change
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
to
f = matlabFunction(a, 'vars', x);
d = matlabFunction(diff(a), 'vars', x);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!