Info

This question is closed. Reopen it to edit or answer.

The use of Sum()

1 view (last 30 days)
Joshua Adame
Joshua Adame on 19 Aug 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
I am not sure how to use the sum command can any one help me understand what I am doing wrong?
  3 Comments
Walter Roberson
Walter Roberson on 19 Aug 2019
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 on 22 Aug 2019
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.

Answers (1)

Nishant Gupta
Nishant Gupta on 23 Aug 2019
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:

Tags

Community Treasure Hunt

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

Start Hunting!