How to integrate using a for loop?

Hello,
I have a homework in which I need to integrate y = cos (t) from 0 to 1 using "for".
This is what I have tried:
fs = 100;
t = [0:1;1];
y = sin(t);
cont = 0;
for i = t(0):1:t(1)
cont = cont + y(i);
end
I am a complete noob using Matlab so any dumb error please be patient. (Ignore the fs).
Thank you.

댓글 수: 5

fs = 100;
% t = [0:1;1];
t = linspace(0,1) ;
y = sin(t);
cont = 0;
for i = 1:length(t)
cont = cont + y(i);
end
But you did is a sum, not integration.
Walter Roberson
Walter Roberson 2016년 11월 9일
"One plus one equals three, for very large values of 'one' and very small values of 'three'."
Integration can be defined as the infinite limit of sums at finer and finer intervals. So what the poster is doing is not improper, even if they should be using trapz or Simpson's instead of just summing.
In essence, integration is a summation, so I believe there's no problem with that. However, the problem lies in the number of samples, i.e. the value of t.
From the limited integration of sin(t) when t = 0 -> 1, we know that the result is 1 - cos(1) = 0.4597 (approximately), so this is the result we should get if everything goes right.
%Verify using Matlab command
fun = @(x) sin(x);
q = integral(fun,0,1)
q =
0.459697694131860 %Basically the same number
However, when using a for loop, the value of cont will change as per the size of the for loop. For example:
t = linspace(0,1); %100 samples
y = sin(t);
cont = 0;
for i = 1:length(t)
cont = cont + y(i);
end
%cont = 45.930420259879121
%For different number of samples
t = linspace(0,1, 10); %10 samples
y = sin(t);
cont = 0;
for i = 1:length(t)
cont = cont + y(i);
end
%cont = 4.553757403387477
So that's your problem right here. As the number of samples increase, the value of cont will increase indefinitely.
Guillaume
Guillaume 2016년 11월 9일
편집: Guillaume 2016년 11월 9일
Hum, No! Integration is not just summing up the values. It's summing up the values multiplied by the interval step, remember the dt when you write the integral expression:
t = linspace(0, 1);
cont = sum(sin(t) .* (t(2)-t(1))) %loop is totally unnecessary
%cont = 0.463943638988678
t = linspace(0, 1, 10);
cont = sum(sin(t) .* (t(2)-t(1)))
%cont = 0.505973044820831
So yes, you can use summation to calculate an integral, but KSSV is right the plain summation that the OP is using is NOT an integral (well, unless your dt is 1).
Mostafa
Mostafa 2016년 11월 9일
You're absolutely right.. I don't believe I actually forgot about that. Well, it's never too late to revise basic math.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2016년 11월 9일

0 개 추천

Not withstanding the fact that the loop is completely unnecessary and the fact that the sum function would produce the same result, your problem has nothing to do with matlab and everything to do with math.
As per the definition of the integral, it's the sum of the values of the function multiplied by the integration step (hence why we write the dt in the integration formula). Not just the sum of the values of the function.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2016년 11월 9일

댓글:

2016년 11월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by