Info
この質問は閉じられています。 編集または回答するには再度開いてください。
The use of Sum()
1 回表示 (過去 30 日間)
古いコメントを表示
I am not sure how to use the sum command can any one help me understand what I am doing wrong?
3 件のコメント
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
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
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:
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!