- #1
stripes
- 266
- 0
Homework Statement
I am to design a small piece of code in Mathematica that takes in four parameters, a number z, the number of intervals n, the starting point, and the ending point. We are basically integrate a function of the form:
[itex]f(x)=(z+1)x^{z}[/itex]
so we don't change the function at all except for the value of z.
Homework Equations
The trapezoidal rule.
The Attempt at a Solution
Here is my code so far, but my instructor is telling me that the calculated numbers/estimates are too high.
Code:
ClearAll["Global`*"]
trapRule[z_: 1, n_: 100, a_: 0, b_: 1] :=
Module[{h, end1, end2, total},
h = (b - a)/n;
end1 = (z + 1)*(a^z);
end2 = (z + 1)*(b^z);
total = h*(end1 + end2);
i = 1.;
While[i <= n,
total += 2*((z + 1)*((a + (i*h))^z));
i++;
];
(total*h)/2
]
I have created my own algorithms, I have looked some up online...they are all the same. I keep getting the same code above but basically it should be much more accurate for a given value of n. I can't use things like summation, I have to just use very basic commands. Again this is being done in Mathematica. Thanks in advance for any help!