Integrate a polyfit-polyval in matlab, I would appreciate any input. I want to calculate the integral of the sine function from a trend li

15 views (last 30 days)
X=[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 ];
Y=[ 0 0.017451121 0.034896927 0.052332105 0.069751344 0.08714934 0.104520792 0.121860412 0.139162917 0.156423038 0.173635518 0.190795114 0.207896601 0.224934771 0.241904433 0.258800419 0.275617584 0.292350805 0.308994987 0.32554506 0.341995983 0.358342746 0.374580371 0.390703911 0.406708457 0.422589134 0.438341105 0.453959573 0.469439781 0.484777014 0.4999666 0.515003915 0.529884377 0.544603456 0.559156667 0.573539579 0.587747811 0.601777035 0.61562298 0.629281427 0.642748218 0.65601925 0.669090481 0.681957931 0.694617681 0.707065874 0.719298721 0.731312494 0.743103535 0.754668253 0.766003125 0.7771047 0.787969596 0.798594504 0.808976189 0.819111488 0.828997314 0.838630657 0.848008582 0.857128234 0.865986835 0.874581687 0.882910172 0.890969753 0.898757976 0.90627247 0.913510945 0.920471196 0.927151104 0.933548635 0.939661839 0.945488856 0.95102791 0.956277314 0.96123547 0.965900868 0.970272086 0.974347793 0.978126748 0.9816078 0.984789889 0.987672046 0.990253392 0.992533143 0.994510602 0.996185169 0.997556332 0.998623675 0.999386873 0.999845692 0.999999993 ];
plot(X,Y)
p2=polyfit(X,Y,4);
p3=polyfit(X,Y,3);
hold on
c=polyval(p2,X);
plot(X,c)
FS=7;
leg = legend(["Seno","Aproximacion Seno"],'Location','northwest','Orientation','horizontal','FontSize',FS,"NumColumns",1);
x1 = 12;
x2 = 12;
leg.ItemTokenSize = [x1, x2];
legend('boxoff');
hold off

Accepted Answer

KSSV
KSSV on 11 Aug 2022
%% Integration
p2=polyfit(X,Y,4);
fun = @(x) p2(1)*x.^4+p2(2)*x.^3+p2(3)*x.^2+p2(4)*x+p2(5) ; % using p2 from polyfit
val = integral(fun,X(1),X(end))
  1 Comment
Juan David Parra Quintero
Juan David Parra Quintero on 11 Aug 2022
Note that the integral must give 1 between 0 and 90 degrees of the sine function, but with the code it gives 57.2. Do you know what error is happening?
X=[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 ];
Y=[ 0 0.017451121 0.034896927 0.052332105 0.069751344 0.08714934 0.104520792 0.121860412 0.139162917 0.156423038 0.173635518 0.190795114 0.207896601 0.224934771 0.241904433 0.258800419 0.275617584 0.292350805 0.308994987 0.32554506 0.341995983 0.358342746 0.374580371 0.390703911 0.406708457 0.422589134 0.438341105 0.453959573 0.469439781 0.484777014 0.4999666 0.515003915 0.529884377 0.544603456 0.559156667 0.573539579 0.587747811 0.601777035 0.61562298 0.629281427 0.642748218 0.65601925 0.669090481 0.681957931 0.694617681 0.707065874 0.719298721 0.731312494 0.743103535 0.754668253 0.766003125 0.7771047 0.787969596 0.798594504 0.808976189 0.819111488 0.828997314 0.838630657 0.848008582 0.857128234 0.865986835 0.874581687 0.882910172 0.890969753 0.898757976 0.90627247 0.913510945 0.920471196 0.927151104 0.933548635 0.939661839 0.945488856 0.95102791 0.956277314 0.96123547 0.965900868 0.970272086 0.974347793 0.978126748 0.9816078 0.984789889 0.987672046 0.990253392 0.992533143 0.994510602 0.996185169 0.997556332 0.998623675 0.999386873 0.999845692 0.999999993 ];
plot(X,Y)
p2=polyfit(X,Y,4);
hold on
time= linspace(0,90,90);
c=polyval(p2,time);
plot(time,c)
FS=7;
leg = legend(["Seno","Aproximacion Seno"],'Location','northwest','Orientation','horizontal','FontSize',FS,"NumColumns",1);
T1 = 12;
T2 = 12;
leg.ItemTokenSize = [T1, T2];
legend('boxoff');
hold off
fun = @(x) p2(1)*x.^4+p2(2)*x.^3+p2(3)*x.^2+p2(4)*x+p2(5) ; % using p2 from polyfit
val = integral(fun,X(1),X(end));

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 11 Aug 2022
https://www.mathworks.com/help/matlab/ref/polyint.html
polyint() the component vector to get the integral of the polynomial, and polyval to evaluate.
  1 Comment
Walter Roberson
Walter Roberson on 12 Aug 2022
You are not integrating sin(x), you are integrating sind(x). sind(x) = sin(alpha*x) for alpha = π/180
syms alpha x
int(sin(alpha*x), x, 0, sym(pi)/2/alpha)
ans = 1/alpha
with alpha being π/180, then 1/alpha is 180/π which is 57 point something

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!