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