Quadrature, midpoint
15 views (last 30 days)
Show older comments
Hello and good day to you all! I'm having a little difficulty in completing the questions in the link below and would be grateful for some assistance in finding solutions.
Up to now I have come up with the following incomplete code:
function [ICMR,h]=midpoint(a,b,n)
h=(b-a)/n ;
for=i=1:n ;
m(i)=a+(i-0.5)*h ;
end
I=-((10*pi-3+3*exp(2*pi))/25/exp(2*pi));
ICMR=h*sum(I);
Now I am looking for someone to help me gain a better understanding of how to use Matlab to complete these questions. please help!!
2 Comments
Jan
on 15 Mar 2011
Please use the code formatting to increase the readability.
My browser can not display the Google-Docs files. It would be easier for us, if you take the time to post the question instead of the link.
Answers (1)
Matt Tearle
on 15 Mar 2011
A few things right off the bat:
- syntax error in your for-loop
- you don't need a for-loop -- just make a vector of the x points using the range operator (:)
- have the function be one of the arguments to the function, then your function values can be evaluated directly as I = f(x) (where x is the vector of points found in step 2). When you call your function, pass f in as a function handle.
- ( EDIT to add:) To do parts (b)-(d) of the question, write a script that makes a vector of h values;
- Use a for-loop to go through the values of h, apply your function, compare to the exact answer, and store the resulting error in another array;
- At the end of the loop, you should have a vector h and a corresponding vector err; use the appropriate semilog* function to plot.
- Don't forget to preallocate space for err before doing 5.
If you really want to get slick, you can replace 5 with an arrayfun call, having defined an anonymous function handle to calculate the error as a function of h.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!