How can i solve following problems?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
Walter Roberson
2020년 12월 26일
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.
PULAK Kumer
2020년 12월 26일
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)));
PULAK Kumer
2020년 12월 27일
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?
Walter Roberson
2020년 12월 27일
I will check later after I wake up
Walter Roberson
2020년 12월 28일
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?
Walter Roberson
2020년 12월 28일
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.
PULAK Kumer
2020년 12월 28일
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);
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
