My code won't run. It says there is an error in line 30.

2 ビュー (過去 30 日間)
Oliver Mendoza
Oliver Mendoza 2020 年 2 月 20 日
回答済み: Alex Mcaulley 2020 年 2 月 20 日
I am trying to make this program run. When I attempt to run it, it says that there is an error in line 30. I don't know what the issue could be.
  1 件のコメント
Jon
Jon 2020 年 2 月 20 日
編集済み: Jon 2020 年 2 月 20 日
What does it say the error is? Please cut and paste the entire error message. Also, please post the code that you use to test/call your function that can be run to produce the error.

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

回答 (1 件)

Alex Mcaulley
Alex Mcaulley 2020 年 2 月 20 日
There is an error in your calls to matlabFunction:
function [] = Newton(f, x1, tol, N)
%
% Newton uses Newton’s method to approximate a root of f(x) = 0.
%
% [r, n] = Newton(f, x1, tol, N), where
%
% f is a symbolic function representing f(x),
% x1 is the initial point,
% tol is the scalar tolerance for convergence (default 1e-4),
% N is the maximum number of iterations (default 20),
%
% r is the approximate root of f(x) = 0,
% n is the number of iterations required for convergence.
%
if nargin < 4 || isempty(N), N = 20; end
if nargin < 3 || isempty(tol), tol = 1e-4; end
% Find f' and convert to MATLAB function for evaluation
% syms x
fp = matlabFunction(diff((f))); %Changed
% Convert f to MATLAB function for evaluation
% syms x
f = matlabFunction(f); %Changed
x = zeros(1,N+1); % Pre-allocate
x(1) = x1;
for n = 1:N
if fp(x(n)) == 0
r ='failure';
return
end
x(n+1) = x(n) - (f(x(n))/fp(x(n)));
if abs(x(n+1) - x(n)) < tol
r = x(n+1);
return
end
end
end

カテゴリ

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