Help please with inline function
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Jan
le 12 Fév 2017
Currently your code is not readable. Please read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Réponses (2)
Walter Roberson
le 12 Fév 2017
f = inline( char(fun) ); %inline the function
z = diff(f(x)); % derive the function of x
f1 = inline( char(z) ); %inline again
0 commentaires
Star Strider
le 12 Fév 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?
0 commentaires
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!