필터 지우기
필터 지우기

How can i solve following problems?

조회 수: 1 (최근 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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by