Help please with inline function

8 visualizzazioni (ultimi 30 giorni)
Abdurahman itani
Abdurahman itani il 12 Feb 2017
Risposto: Star Strider il 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

Risposte (2)

Walter Roberson
Walter Roberson il 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 il 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?

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by