Help please with inline function

조회 수: 8 (최근 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

답변 (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 CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by