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 个评论

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?

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by