central, forward and backward differences

I wanted to make an input function for the user to add function and then the the script derives that function and then executes the rest of the code however i cannot get it to make an input function i will state where the errors are when i make an input
inp=input('function: ');
x=linspace(0,pi/2,10);
syms x; %error
y=inp; %error
dy=diff(y); %error
dydx_analytical=dy;
dydx_approx=diff(y)./diff(x); %error
dydx_approx(length(x))=NaN;
percent_error=(dydx_analytical-dydx_approx)./dydx_analytical*100;
table=[x; dydx_analytical;dydx_approx;percent_error]; %error
disp('Forward Difference Approximation of the derivative sin(x)' )
disp(' x dy/dx dy/dx %error')
disp(' forward approximation ')
fprintf('%8.4f\t%8.4f\t%8.4f\t%8.4f\n', table)
plot(x,dydx_approx,'s--')
hold on
plot(x,dydx_analytical)
hold off
legend('Forward Difference','Analytical')

1 Comment

You declared x as numeric array then you are trying to mke it symbolic
x=linspace(0,pi/2,10);
syms x; %error
Can you show the way you input the function?

Sign in to comment.

Answers (0)

Categories

Products

Tags

Asked:

on 3 Apr 2021

Commented:

on 3 Apr 2021

Community Treasure Hunt

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

Start Hunting!