Advertisement
makispaiktis

Monomaxos + Curve Fitting in 100 points with poly8

Sep 15th, 2023
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.06 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5. %% 1. Calculate the maximum money
  6. A = 10^4;
  7. N = 100;
  8. nums = 1 : N;
  9. x = 1 ./ nums;
  10. SUM1 = sum(x);
  11. max_money1 = A * SUM1;
  12.  
  13. syms t
  14. func = 1 / t;
  15. SUM2 = int(func, t, 1, N);
  16. SUM2 = vpa(floor(10^4 * SUM2) / 10^4);
  17. max_money2 = A * SUM2;
  18.  
  19. fprintf("<strong>With sum of " + num2str(N) + "-term series:</strong>\n")
  20. SUM1
  21. max_money1 = vpa(max_money1)
  22. fprintf("\n<strong>With integral from 1 to " + num2str(N) + ":</strong>\n")
  23. SUM2
  24. max_money2 = vpa(max_money2)
  25. fprintf("\n\n")
  26.  
  27.  
  28.  
  29. %% 2. Fit data - Approach the 100-term series as a polynomial
  30. y = aggregate(x);
  31.  
  32. [xData, yData] = prepareCurveData(nums, y);
  33. ft = fittype('poly8');
  34. [fitresult, gof] = fit(xData, yData, ft, 'Normalize', 'on');
  35.  
  36. figure('Name', 'untitled fit 1');
  37. h = plot(fitresult, xData, yData);
  38. legend(num2str(N)+" Real points", "Fit with poly8", num2str(N)+"-term Series")
  39. hold on
  40. plot(nums, y, 'blue')
  41.  
  42.  
  43.  
  44. %% AUXILIARY FUNCTION
  45. function agg = aggregate(vec)
  46.     agg(1) = vec(1);
  47.     for i = 2 : length(vec)
  48.         agg(i) = agg(i-1) + vec(i);
  49.     end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement