필터 지우기
필터 지우기

summing to create a vector

조회 수: 2 (최근 30일)
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 3일
댓글: Walter Roberson 2016년 6월 5일
I'm trying to write a function that calculates sum of ((-1)^k *sin((2k+1)t))/((2k+1)^2 ( for k=0 to n) t varies from 0 to 4*pi with 1001 values its supposed to return a vector with size n this is the code i wrote
function [summ ]= triangle_wave (n)
for k=1:n;
for t= linspace(0,4*pi,1001);
summ=sum((-1)^k*sin((2*k+1)*t))/((2*k+1)^2);
end
end
end
it keeps outputting the last calculate sum instead of adding each sum to the vector . what can i add to this code to achieve that ?

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 3일
function [summ ]= triangle_wave (n)
t = linspace(0,4*pi,1001);
summ = zeros(n+1, length(t));
for k = 1 : n;
summ(k+1,:) = summ(k,:) + (-1).^k .* sin((2*k+1) .* t)) ./ ((2*k+1).^2);
end
summ = summ(2:end,:);
end
  댓글 수: 2
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 3일
perfect, thanks a lot
Walter Roberson
Walter Roberson 2016년 6월 5일
Instead of looping over the values of t, I use t as a vector, operating on all of the elements at once. Each iteration through I create an entire vector of values, the application of the formula with one particular k to the entire set of t values. Normally I would just add all of those together over all of the k, but you wanted to have all of the intermediate results for all of the different k, so it is necessary to store all of the results along the way.
The result for k = 1 is stored in row 2, the result for k = 2 is stored in row 3, and so on, until at the end one more row than n has been produced. You then omit the first row and take the rest as your answer.
The reason you do it this way is to make the code easier because each step involves adding to what the step before produced while still keeping what was produced in the previous step. That's easy to think of, but you have the practical difficulty of handling the very first output. You could code like
if k == 1
summ(k,:) = (-1).^k .* sin((2*k+1) .* t)) ./ ((2*k+1).^2);
else
summ(k,:) = summ(k-1,:) + (-1).^k .* sin((2*k+1) .* t)) ./ ((2*k+1).^2);
end
to avoid having to use the extra row, but you can see that you had to use special handling for the first row because for the first row there is no "previous" to add on to. The code is more compact if you do it the way I did, initialize a row with 0 to be there as the "previous" row.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 3일
n=20
t= linspace(0,4*pi,1001);
for k=1:n
s(k)=sum((-1)^k *sin((2*k+1)*t)/((2*k+1)^2)) ;
end
s
  댓글 수: 2
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 3일
great! you guys are the best|
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 3일
편집: Walter Roberson 2016년 6월 3일
I'm sorry there is still an error here , the inside loop is supposed to b k, while the outer loop be t, such that it produces a vector of 1001 elements as opposed to 20. I've tried to swap them in the code you provided but it keep sending 20 elements vector back to me .
the code i should have posted earlier is :
function [summ ]= triangle_wave (n)
for t= linspace(0,4*pi,1001);
for k=1:n;
summ=sum((-1)^k*sin((2*k+1)*t))/((2*k+1)^2);
end
end
end
thanks

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by