Chebyshev method for finding root

조회 수: 2 (최근 30일)
Milind Amga
Milind Amga 2020년 10월 2일
댓글: Milind Amga 2020년 10월 2일
This code is for finding root of an equation. But this runs into an error saying, unable to convert expression into double array. The error occurs because of the improper use of function handle in g,M,N.
If I take input 'f' as a function handle then I cannot differentiate it and if I take 'f' as just an expression then I cannot convert it into a function handle.
Can someone guide me in the right direction?
Thank you in advance for your time and effort.
function [root,iteration] = chebyshev(a,f) %please input function in terms of y=f(x)(x as independent variable)
% Do not input function as function handle
if nargin<1 % check no of input arguments and if input arguments is less than one then puts an error message
fprintf('Error! Atleast one input argument is required.');
return;
end
if nargin<2 % check no of input arguments and if input arguments is less than two then puts a=0
a=0;
end
g = @(x) f;
M = @(x) diff(f,'x');
N = @(x) diff(f,'x',2);
temp = false;
i =1 ;
x(1) = a;
count = 0;
while temp == false
A = g(x(i));
B = M(x(i));
C = N(x(i));
D = double((A/B) + 0.5*((A^2)*C/(B^3)));
x(i+1) = x(i) - D;
if x(i+1)-x(i) == 0
temp = true;
root = x(i-1);
iteration = i-1;
return;
end
i = i+1;
count = count + 1;
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 2일
% Do not input function as function handle
The code says right there not to pass in a function handle for f.
M = @(x) diff(f,'x');
That will fail for function handle f.
You would need to be passing in a symbolic expression or symbolic function to have a chance.
  댓글 수: 1
Milind Amga
Milind Amga 2020년 10월 2일
Yes, I totally understood what's causing the error but couldn't think of any solution.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by