Quadrature, midpoint

13 Ansichten (letzte 30 Tage)
Thomas Davidson
Thomas Davidson am 15 Mär. 2011
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 Kommentare
Jan
Jan am 15 Mär. 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.
Matt Tearle
Matt Tearle am 15 Mär. 2011
basically "write a mid-point rule quadrature function"

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt Tearle
Matt Tearle am 15 Mär. 2011
A few things right off the bat:
  1. syntax error in your for-loop
  2. you don't need a for-loop -- just make a vector of the x points using the range operator (:)
  3. 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.
  4. ( EDIT to add:) To do parts (b)-(d) of the question, write a script that makes a vector of h values;
  5. 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;
  6. At the end of the loop, you should have a vector h and a corresponding vector err; use the appropriate semilog* function to plot.
  7. 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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by