I am new to MATLAB and I am encountering a bit of trouble writing a program to calculate a an integral which itself depends on functions that get integrated each time the integrand is called.
I have already written a function for the integrand, which, when called for a given value of its argument, yields the result that I expect. However, when I try to integrate over this argument, I get multiple errors about disagreeing matrix dimensions. The strange thing is that I can evaluate the integrand at all values for the argument.
My code is as follows:
%DECLARATION OF q1 and Q1
function y = q1(w, tt)
y = exp(-tau*w).*sin(tt.*w)./w;
function y = Q1(tt)
y = integral(@(w) q1(w, tt), 0, Inf, 'AbsTol', 1e-12);
%DECLARATION OF q2 and Q2
function y = q2(w, tt)
if (tt*w) < 1e-5
y = (1/24.0)*exp(-tau.*w).*tt.*tt.*(12.0 - w.*w.*tt.*tt).*coth(0.5*w).*w;
y = exp(-tau.*w).*(1.0 - cos(tt.*w)).*coth(0.5*w)./w;
function y = Q2(tt)
y = integral(@(w) q2(w, tt), 0, Inf, 'AbsTol', 1e-12);
function y = Integrand(tt)
y = cos(Omega*tt + Q1(tt)).*exp(-Q2(tt));
function y = Rate(t)
y = integral(@(tt) Integrand(tt), 1e-3, t);
I appreciate any feedback you can give me.
For efficiency (and it *really* matters for that), MATLAB tries to
vectorize the calls to the integrand function. If you read the doc, it
will tell you that your integrand function must accept an *array* of
inputs and return an array of corresponding outputs. If you have a
function which cannot do that, and most of your functions cannot, you
have a couple of options. The simplest is to regard your problem as a
vector-valued integral with just 1 component. When integrating
vector-valued integrands, INTEGRAL only passes scalars to the integrand
function. To make that work, simply append the option-value pair
to the argument list for each call to INTEGRAL.
The other approach (and almost always a faster one) is to use ARRAYFUN
to vectorize a function that only accepts scalars. So, for example, to
vectorize q2, you might use
q2v = @(w,tt)arrayfun(@q2,w,tt);
instead of q2 in the definition of Q2. I do think ARRAYFUN can get
confusing, so if I were you I would use the 'ArrayValued' option *first*
and then try to remove those one at a time by introducing uses of ARRAYFUN.
I notice that you are only supplying 'AbsTol' below. That is probably a
mistake. INTEGRAL attempts to meet a *mixed* error control condition
involving an estimate of the relative error. Absolute error control is
only relevant when the true answer approaches zero. A good way of
thinking about it is that the 'RelTol' indicates how many significant
digits you want (e.g. 1e-5 means about 5 significant digits, or almost
that), and 'AbsTol' is a kind of "threshold", a "good enough" error when
it becomes difficult to achieve the requested relative tolerance because
cancellation of positive and negative areas lead to a small correct
answer. Technically, only 'AbsTol' only matters if |Q| < AbsTol/RelTol
and only 'RelTol' matters otherwise. The default RelTol is 1e-6, so
when combined with the default RelTol, an AbsTol of 1e-12 only matters
if |Q| < 1e-12/1e-6 = 1e-6.
Finally, you will probably observe that when nesting integrals you will
get more accuracy than you ask for. That sounds like a good thing, but
it is not coming for free. There are a couple of reasons for it, one
being that INTEGRAL has a conservative initial mesh (meaning that there
are quite a few evaluations points at a minimum). The other reason I
suspect is more technical and has to do with truncation error associated
with cross-product terms.
> I am new to MATLAB and I am encountering a bit of trouble writing a
> program to calculate a an integral which itself depends on functions
> that get integrated each time the integrand is called.
> I have already written a function for the integrand, which, when called
> for a given value of its argument, yields the result that I expect.
> However, when I try to integrate over this argument, I get multiple
> errors about disagreeing matrix dimensions. The strange thing is that I
> can evaluate the integrand at all values for the argument.
> My code is as follows:
> %DECLARATION OF q1 and Q1
> function y = q1(w, tt)
> y = exp(-tau*w).*sin(tt.*w)./w;
> function y = Q1(tt)
> y = integral(@(w) q1(w, tt), 0, Inf, 'AbsTol', 1e-12); end
> %DECLARATION OF q2 and Q2
> function y = q2(w, tt)
> if (tt*w) < 1e-5
> y = (1/24.0)*exp(-tau.*w).*tt.*tt.*(12.0 -
> y = exp(-tau.*w).*(1.0 - cos(tt.*w)).*coth(0.5*w)./w;
> function y = Q2(tt)
> y = integral(@(w) q2(w, tt), 0, Inf, 'AbsTol', 1e-12); end
> %RATE INTEGRAL
> function y = Integrand(tt)
> y = cos(Omega*tt + Q1(tt)).*exp(-Q2(tt));
> function y = Rate(t)
> y = integral(@(tt) Integrand(tt), 1e-3, t);
> I appreciate any feedback you can give me.
You can think of your watch list as threads that you have bookmarked.
You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.
To add items to your watch list, click the "add to watch list" link at the bottom of any page.
To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.
You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.
To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.
To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.
No tags are associated with this thread.
A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.
Anyone can tag a thread. Tags are public and visible to everyone.
The newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.
Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.
Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.
There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.
You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.
Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.
Your MATLAB Central account is tied to your MathWorks Account for easy access.
Use the Email Address of Your Choice
The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.
Most newsgroup spam is filtered out by the MATLAB Central Newsreader.
Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.
Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.