Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

The use of Sum()

조회 수: 1 (최근 30일)
Joshua Adame
Joshua Adame 2019년 8월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
I am not sure how to use the sum command can any one help me understand what I am doing wrong?
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 8월 19일
line() is only for drawing lines on the display.
You are passing in [x(1); x(9)] for the x component and [y(1); y(9)] for the y component, and you are doing that no matter what the value of i is. It is questionable what x(9) and y(9) are going to hold before you assign to x(i) and y(9) when i reaches 9. Effectively you are going to draw only two different lines: the same line for i = 1 to 8, and then a second different line when i reaches 9 so you overwrite x(9) and y(9), and the second line will be repeated for the remainder of the i iterations. It is not obvious why you are doing this.
Nishant Gupta
Nishant Gupta 2019년 8월 22일
Hi Joshua
First of all you cannot pass a line to the sum( ) function and can you send me the full code script so that I can help you further.

답변 (1개)

Nishant Gupta
Nishant Gupta 2019년 8월 23일
Hi Joshua
Like I previously mentioned in my comment, you cannot pass a line to the "sum( )" function. From the code it seems that you are trying to calculate the area under the given curve which is basically integration.
Refer to the following modified code script, it will help you :
x(1)= 0;
x(n)= 1;
h= 0.001;
n = (x(n) - x(1))/h;
area = 0;
sum1 = 0;
for i=1:n
x(i + 1) = x(i) + h;
y(i) = -9 * exp(-10 * x(i)) + 18 * exp(-15 * x(i))
area = y(i) * h;
sum1 = sum1 + area;
end
For better accuracy, I have decreased the step size "h" to 0.001
As a second alternative, you can directly use the integral function referred in the following link:

태그

Community Treasure Hunt

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

Start Hunting!

Translated by