Help please with inline function

9 ビュー (過去 30 日間)
Abdurahman itani
Abdurahman itani 2017 年 2 月 12 日
回答済み: Star Strider 2017 年 2 月 12 日
hi everyone
i am trying to run this code in matlab
clear all
clc
syms x;
fun=input('f(x):'); %enter any function you want to derive
f=inline(fun); %inline the function
z=diff(f(x)); % derive the function of x
f1=inline(z); %inline again
x0=input('Enter the iniial value of interval:');
x=x0; % store in x
for i=0:1000
y=x; %stores x value in y
x=y-[f(x)/f1(x)];
if x==y % compares the new value with the old value
break
end
end
fprintf( 'The total number of iterations are:');
i
x
but this is the error I am getting please help me
f(x):(x^2)+4
??? Error using ==> inline.inline at 47
Input must be a string.
Error in ==> Newton at 7
f=inline(fun); %inline the function
  1 件のコメント
Jan
Jan 2017 年 2 月 12 日

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

回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 2 月 12 日
f = inline( char(fun) ); %inline the function
z = diff(f(x)); % derive the function of x
f1 = inline( char(z) ); %inline again

Star Strider
Star Strider 2017 年 2 月 12 日
If you have R2012a or later, use symfun instead of inline, or even an anonymous function. See the documentation for symfun for details.
To use symfun,, your inplut and function declaration would then be:
syms x
fun=input('f(x):'); %enter any function you want to derive
f = symfun(sym(fun), x)
and for the response of the input:
f(x):x^2 + 2*x + 1
the symfun result is:
f(x) =
x^2 + 2*x + 1
and you have a useful symbolic function!
What version of MATLAB are you using?

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by