How can i solve following problems?

2 ビュー (過去 30 日間)
PULAK Kumer
PULAK Kumer 2020 年 12 月 26 日
コメント済み: Walter Roberson 2020 年 12 月 28 日
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?

採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 26 日
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 件のコメント
PULAK Kumer
PULAK Kumer 2020 年 12 月 28 日
How can i use matlab function instead of inline here?
Walter Roberson
Walter Roberson 2020 年 12 月 28 日
Change
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
to
f = matlabFunction(a, 'vars', x);
d = matlabFunction(diff(a), 'vars', x);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by