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 commentaire

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?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by