SHARE
TWEET

Untitled

a guest Aug 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clear;
  2. format long;
  3. x = [1 2 3 4 5];
  4. y = [2 5 9 12 20];
  5. x_at = 1;
  6.  
  7. range = length(x);
  8. h = x(2)-x(1);
  9. p_val = (x_at-x(1))/h;
  10.  
  11. %finding index of x_at
  12. x_index = 1;
  13. for i=1:range
  14.     if(x_at == x(i))
  15.         x_index = i;
  16.         break;
  17.     end
  18. end
  19.  
  20. %forward diference table
  21. new_y = y(x_index:range);
  22. diff_y = zeros(1,range-x_index);
  23. for i=1:range-x_index
  24.     temp = diff(new_y,i);
  25.     diff_y(i) = temp(1);
  26. end
  27.  
  28. disp(diff_y);
  29.  
  30.  
  31. %first order derivative
  32. syms p;
  33. product_p = 1;
  34. sum = 0;
  35. for i=1:range-x
  36.     product_p = product_p * (p-(i-1));
  37.     diff_p = diff(product_p,1);
  38.     co_eff = (diff_p /factorial(i)) * diff_y(i);
  39.     eachtermval = vpa(subs(co_eff,p,p_val))
  40.     sum = sum +eachtermval;
  41.    
  42. end
  43. first = sum/h;
  44. disp(first);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top