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 个评论
KSSV
2016-11-9
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
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.
Mostafa
2016-11-9
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.
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
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
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!