Help please with inline function

8 views (last 30 days)
Abdurahman itani
Abdurahman itani on 12 Feb 2017
Answered: Star Strider on 12 Feb 2017
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

Answers (2)

Walter Roberson
Walter Roberson on 12 Feb 2017
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 on 12 Feb 2017
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?

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!