Advertisement
mrlantan

Untitled

Nov 2nd, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.04 KB | None | 0 0
  1. function main
  2. clc
  3. format long
  4.  
  5.     step = 0.01;
  6.     cur_x = 0.0;
  7.     derivs_my_1 = [];
  8.     derivs_my_2 = [];
  9.     derivs_1 = [];
  10.     derivs_2 = [];
  11.     syms xx;
  12.     derivative1 = diff(tan(2 * xx))
  13.     derivative2 = diff(derivative1)
  14.     my_func = @func
  15.     while (cur_x <= 1.0)
  16.         derivs_1 = [derivs_1, vpa(subs(derivative1, xx, cur_x))];
  17.         derivs_2 = [derivs_2, vpa(subs(derivative2, xx, cur_x))];
  18.         derivs_my_1 = [derivs_my_1, calc_deriv(cur_x, my_func, 1)];
  19.         derivs_my_2 = [derivs_my_2, calc_deriv(cur_x, my_func, 2)];
  20.         cur_x = cur_x + step;
  21.     end
  22.     derivs_2
  23.     derivs_my_2
  24. end
  25.  
  26. function F = func(x)
  27. %     write your stupid function here
  28.     F = tan(2 * x);
  29. end
  30.  
  31. function F = calc_deriv(x, func, power)
  32.     increment = 1e-5;
  33.     if power == 1
  34.         F = (func(x + increment) - func(x)) / increment;
  35.     else
  36.         f1 = (func(x + increment) - func(x)) / increment;
  37.         f2 = (func(x + 2 * increment) - func(x + increment)) / increment;
  38.         F = (f1 - f2) / increment;
  39.     end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement