Quadrature, midpoint

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 件のコメント

Jan
Jan 2011 年 3 月 15 日
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 2011 年 3 月 15 日
basically "write a mid-point rule quadrature function"

サインインしてコメントする。

回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 3 月 15 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2011 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by