%%%%%%%%% Illustrations of the symbolic toolbox for calculus %%%% %%%% File is to be used by extracting a few lines at a time (in order) %%%% into %%%% the workspace %%%% Creating symbolic variables x y clear x y syms x y %%% differentiaton f = 4*x^4-2*x+1 f_deriv = diff(f) g = (x+1)/(exp(x)*sin(x) + x^2) g_deriv = diff(g) %%% more differentiation fun1 = x^2+3*x+4 df1_dx = diff(fun1) df1_dx2 = diff(df1_dx) fun1_restored = int(df1_dx) %%% more differentiation fun2 = cos(y)+tan(y)-sin(y) df2_dy = diff(fun2) fun3 = x*sin(x)*exp(x)/cos(x) df3_dx = diff(fun3) df3_dx_new = simplify(df3_dx) %% integration intf=int(f) intf2=int(fun2) %% Find a tangent curve from a function syms x a=4; f = 4*x^4-2*x+1-sin(2*x) dfdx=diff(f); f_at_a=subs(f,a); dfdx_at_a=subs(dfdx,a); tangent=f_at_a+(x-a)*dfdx_at_a %%%%%% Numerators and denominators g1 = (x+1)/(x^2-4) [num_g1,den_g1] = numden(g1) g2 = g1+y^2/sin(y) [num_g2,den_g2] = numden(g2) %%%%%%%% matrix algebra with symbolic expressions A=[x,y^2;x-1,x+y] inverse_of_A = inv(A) determinant_of_A = det(A) %%%%% Taylor series %% The syntax of TAYLOR has changed since video was made. %% %% See doc taylor for details. g1 = (x+1)/(x^2-4) g1_taylor_about_zero = taylor(g1,'ExpansionPoint',0,'Order',5) %%% 4th order g1_taylor_about_one = taylor(g1,'ExpansionPoint',1,'Order',4) %%% 3rd order xval=-1:.02:1; ytay = subs(g1_taylor_about_zero,xval); yval = subs(g1,xval); plot(xval,yval,'b',xval,ytay,'r'); legend('Actual function','4th order Taylor series'); taylor(sin(y+2),'order',6) %%%%% Symbolic expression from a string syms x f='x^2+3*x'; fx=eval(f); whos f fx diff(fx) diff(f) %%%% Creating symbolic variables x w y z syms x y w z k %%%%%% Numerators and denominators g1 = (x+1)/(x^2-4) [num_g1,den_g1] = numden(g1) g2 = g1+y^2/sin(y) [num_g2,den_g2] = numden(g2) z = cos(x)*sin(y) % What do you get ? w = tan(z) % What do you get whos %%%%% Summation sum_1_to_20 = symsum(k,0,20) %%%% Does 0+1+2+....+20 sum_sqrt = symsum(sqrt(k),2,5) %%%% Does sqrt(2)+sqrt(3)+...+sqrt(5) value = eval(sum_sqrt) %%% Simplification and factorisation of functions clear i simple f1 = sin(x)^2+cos(x)^2 simple_f1 = simplify(f1) f2 = cos(x)+i*sin(x) simple_f2 = simplify(f2) f3 = y*(y+2)-y^3+y^2*(y-2)+4-2*y simple_f3=simplify(f3) factors_f3 = factor(simple_f3) ex_f3 = expand(factors_f3) expand_sine = expand(sin(y+2*z)) %%%%%%%% Evaluation of a symbolic expression h1 = sin(x)+x^2/7-0.3 g1 = cos(x); x_values = 0:.04:4; y_values=subs(h1,x_values); figure(1);clf reset plot(x_values,y_values); plot(x_values,y_values,'b',x_values,subs(g1,x_values),'r'); %%%% Substitute in for only one of the symbolic values g2=cos(x)+y; subs(g2,x,3) %%% Multiple substitutions subs(g2,{x,y},{0:.1:1,1:.1:2})