Solve equation with for loop function

조회 수: 13 (최근 30일)
Annique Coetzer
Annique Coetzer 2022년 8월 4일
댓글: John D'Errico 2023년 6월 18일
DAETERMINE IF BOTH IDENTITIES ARE CORRET FOR N=100 USING THE FOR LOOP FUNCTION IN MATLAB
  댓글 수: 3
Torsten
Torsten 2022년 8월 4일
Now you know at least that both formulas are true -:)
syms k n
s1 = symsum(k^3,k,1,n)
s1 = 
s2 = symsum((-1)^(k+1)*k^2,k,1,n)
s2 = 
Walter Roberson
Walter Roberson 2022년 8월 4일
syms k n
s3 = symsum((-k)^5, k, 1, n)
s3 = 
subs(s3, n, 10)
ans = 
k = 1:10;
sum((-k).^5)
ans = -220825
Now, how could one write a summation as a for loop ?

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

답변 (1개)

Shantanu Dixit
Shantanu Dixit 2023년 6월 18일
Hi Annique,
Verify both the identites using for loop (using n = 100) and the identity formula.
1.
n = 100;
sum_n_cubes = 0;
% Compute the sum of cubes using a for loop
for i = 1:n
sum_n_cubes = sum_n_cubes + i^3;
end
disp(['Sum of first ', num2str(n), ' cubes is: ', num2str(sum_n_cubes)]);
Sum of first 100 cubes is: 25502500
% Compute the sum of cubes using identity
sum_n_cubes_identity = (n*(n+1)/2)^2;
disp(['Sum of first ',num2str(n),' cubes with identity: ',num2str(sum_n_cubes_identity)]);
Sum of first 100 cubes with identity: 25502500
2.
n = 100;
sum_n_series = 0;
% Compute the sum of cubes using a for loop
for i = 1:n
sum_n_series = sum_n_series + (-1)^(i+1)*i^2;
end
disp(['Sum of series using for loop: ', num2str(sum_n_series)]);
Sum of series using for loop: -5050
% Compute the sum of cubes using identity
sum_n_series_identity = (-1/2)*((-1)^n)*(n*(n+1));
disp(['Sum of series using identity: ', num2str(sum_n_series_identity)]);
Sum of series using identity: -5050
  댓글 수: 1
John D'Errico
John D'Errico 2023년 6월 18일
@Shantanu Dixit - please don't do homework assignments for students when they have made no effort to do anything themselves. This only teaches the student that there is always some sucker willing to do their work for them. This is not a lesson they should learn here, or anywhere.
It does not help the student, teaching them only to post another homework assignment with no effort.
It does not help the site, infact, it actively hurts the site, since it then teaches other students to post their questions too.

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

카테고리

Help CenterFile Exchange에서 Error Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by