Currently, I am working a project with Matlab progrma. I have one problem with the summation variable. This is due lack of experience before. So, could you help me please to code this function in Matlab code: ( ∑_{ i=1}^{n} (f(x_{i})-y_{i} )^2). Look forward to hearing from you soon.

 채택된 답변

Image Analyst
Image Analyst 2015년 6월 7일

0 개 추천

Like I said in your duplicate question:
theSum = 0;
for i = 1 : n
theSum = theSum + f(x(i) - y(i));
end
where f is your function.

댓글 수: 2

John Gebre
John Gebre 2015년 6월 7일
편집: Image Analyst 2015년 6월 7일
Thank you very much for your answer. However, how can I define x(i) and y(i) from i = 1:n (i.e in Matlab code)?
If you want x and y to both be vectors 1,2,3,4,5,....n then just do this:
x = 1:n;
y = 1:n;

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 7일

0 개 추천

Assuming that your function f is vectorizable, and assuming that x_ and y_ are of length n, then
sum((f(x_)-y_).^2)
if they are a different length then
sum((f(x_(1:n))-y_(1:n)).^2)
If your f is not vectorizable, but (for simplicity) assuming that x_ and y_ are the right length,
sum( (arrayfun(@f, x_) - y_).^2 )

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 6월 7일

댓글:

2015년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by