I'm not understanding why this error occurs: Error: Unexpected MATLAB expression

3 ビュー (過去 30 日間)
Matt
Matt 2013 年 10 月 17 日
コメント済み: Jan 2013 年 10 月 17 日
What i entered in console and received
>> newton(x^3-x,3x^2-1,-1,1e-5,20)
??? newton(x^3-x,3x^2-1,-1,1e-5,20)
|
Error: Unexpected MATLAB expression.
function used is as follows
function [ x, ex ] = newton( f, df, x0, tol, nmax )
%
% NEWTON Newton's Method
% Newton's method for finding successively better approximations to the
% zeroes of a real-valued function.
%
% Input:
% f - input funtion
% df - derived input function
% x0 - inicial aproximation
% tol - tolerance
% nmax - maximum number of iterations
%
% Output:
% x - aproximation to root
% ex - error estimate
%
% Example:
% [ x, ex ] = newton( 'exp(x)+x', 'exp(x)+1', 0, 0.5*10^-5, 10 )
if nargin == 3
tol = 1e-4;
nmax = 1e1;
elseif nargin == 4
nmax = 1e1;
elseif nargin ~= 5
error('newton: invalid input parameters');
end
f = inline(f);
df = inline(df);
x(1) = x0 - (f(x0)/df(x0));
ex(1) = abs(x(1)-x0);
k = 2;
while (ex(k-1) >= tol) && (k <= nmax)
x(k) = x(k-1) - (f(x(k-1))/df(x(k-1)));
ex(k) = abs(x(k)-x(k-1));
k = k+1;
end
end

採用された回答

Jan
Jan 2013 年 10 月 17 日
編集済み: Jan 2013 年 10 月 17 日
You are missing a multiplication operator:
Try
newton(x^3-x,3 * x^2-1,-1,1e-5,20)
^
inserted
Edit: I just noticed, that the function header of newton obviously expects strings ast input arguments. Like in the example given:
[ x, ex ] = newton( 'exp(x)+x', 'exp(x)+1', 0, 0.5*10^-5, 10 )
You will have to alter your function call accordingly. However, the Matlab error resulted from the missing multiplication operator.
  2 件のコメント
Matt
Matt 2013 年 10 月 17 日
Thank you very much, something so simple yet can be a pain. The life of syntax...
Jan
Jan 2013 年 10 月 17 日
And the error message has been very specific.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by