필터 지우기
필터 지우기

How to use sum series in Matlab

조회 수: 29 (최근 30일)
Atinesh S
Atinesh S 2015년 4월 10일
댓글: Jonathan Campelli 2015년 4월 13일
I need to use sum series in some assignments. But don't know the syntax
I'm just trying to write sum series as below
Code
k = sum(i^2, i = 1..4)
But I'm getting this error message
The expression to the left of the equals sign is not a valid target for an assignment.
  댓글 수: 1
Stephen23
Stephen23 2015년 4월 11일
The documentation for MATLAB is really rather good, and includes lots of complete, working examples:
You should look at it one day. Looking at the documentation would immediately give a much better idea of how to use the function max than just inventing some syntax. After all, the people who wrote that function are probably the best ones to describe how it can be used.

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

채택된 답변

Jonathan Campelli
Jonathan Campelli 2015년 4월 10일
I'm providing another solution to address the longer summation you have posted in my first answer. Please let me know if this suits your needs:
data = load('ex1data1.txt');
y = data(:, 2);
m = length(y);
X = [ones(m, 1), data(:,1)];
theta = zeros(2,1);
i=1:m;
J=sum(((theta(1)+theta(2).*X(i,2))-y(i)).^2)
Best,
Jonathan
  댓글 수: 3
Atinesh S
Atinesh S 2015년 4월 11일
Ohh little mistake, forgot to multiply the whole expression with "(1/(2*m))"
Jonathan Campelli
Jonathan Campelli 2015년 4월 13일
I apologize for the mistake, and I'm glad you found it even though I was away.

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

추가 답변 (3개)

Image Analyst
Image Analyst 2015년 4월 10일
If you want to use a for loop, try this:
theSum = 0;
for i = 1 : 4
theSum = theSum + i ^ 2;
end
Or if you want a vectorized solution, try this:
i = 1 : 4;
theSum = sum(i .^ 2)
  댓글 수: 2
Atinesh S
Atinesh S 2015년 4월 10일
Here's the complete code I'm using
data = load('ex1data1.txt');
% 'ex1data1.txt' text file containing list of numbers in two
% columns separated by commas
y = data(:, 2); % Storing column 2 of text file in vector array y
m = length(y);
X = [ones(m, 1), data(:,1)];
% Storing column 1 of text file in vector array X and appending all 1's
% column to the vector array
theta = zeros(2,1);
J = symsum(((theta(1)+theta(2)*X(i,2))-y(i))^2, i, 1, m)
Image Analyst
Image Analyst 2015년 4월 10일
OK, but that looks totally different than your original post where you mentioned nothing about y, m, X, J, data, or theta. All you mentioned was "i" and the sum that you called "k".
I don't have any symbolic toolboxes. Is that a requirement that you do it that way rather than the simple numerical way?

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


Jonathan Campelli
Jonathan Campelli 2015년 4월 10일
Hello Atinesh
syms i %Creat symbolic variable 'i'
symsum(i^2, i, 1, 4) %Use symsum to run your summation operation
Best regards,
Jonathan Campelli

Atinesh S
Atinesh S 2015년 4월 10일
Hello Jonathan Campelli, It worked but actually I've to write this below series
While using the code below
J = symsum(((theta(1)+theta(2)*X(i,2))-y(i))^2, i, 1, m)
I'm getting new error message
_Subscript indices must either be real positive integers or logicals.
Error in Test (line 7) J = symsum(((theta(1)+theta(2)*X(i,2))-y(i))^2, i, 1, m)_
  댓글 수: 4
Atinesh S
Atinesh S 2015년 4월 10일
Here's the complete code I'm using
data = load('ex1data1.txt');
% 'ex1data1.txt' text file containing list of numbers in two
% columns separated by commas
y = data(:, 2); % Storing column 2 of text file in vector array y
m = length(y);
X = [ones(m, 1), data(:,1)];
% Storing column 1 of text file in vector array X and appending all 1's
% column to the vector array
theta = zeros(2,1);
J = symsum(((theta(1)+theta(2)*X(i,2))-y(i))^2, i, 1, m)
Jonathan Campelli
Jonathan Campelli 2015년 4월 10일
I am unable to call X(i) using symsum. If you are able, Image Analyst has provided vectorized computations that are a lot cleaner.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by