Solved

# Evaluating a double integral in Matlab

Posted on 2005-05-12
1,893 Views
I have a function of four variables (x,y,t,k) and want to evaluate the finite integral with respect to two of them (t and k) and plot the resulting function with respect to x and y. How can I do this using Matlab?

My function is as follows:

f = k * exp(-k^2*1/2) * cos( x * k * cos(t) + y * k * sin(t) )

I want to evaluate the double integral with (0 < t < 2pi) and (1 < k < 2) and plot the resulting function with respect to x and y.

Can anybody help me?
Thanks :)
T
0
Question by:tryso

LVL 27

Expert Comment

I am nmore familier with MathCad than Mathlab. So you want your plot to be a 3-D plot of f(x,y) vs x and y?
0

Author Comment

Yes,

Which way you plot it is not the main issue though, it might just as well be a contour plot, or even a plot along one axis.

Regards,
Trygve
0

LVL 27

Expert Comment

Yes, I just wanted to make sure I understood what you were looking for. Unfortunately I am not familiar with matlab, just MathCad, so I cannot help you much.
0

LVL 1

Accepted Solution

Hey,
This is the fastest solution I can come up with. Maybe not the best one.

Create a M-file of your function like this: replacing x<->i, y<->j

function z = integrand( k,t)
global x y i j
z=k .* exp(-k.^2*1/2) .* cos( x(i) * k .* cos(t) + y(j) * k .* sin(t) );

now, in your main program write this:

global x y i j %for global variables
x=[0:0.5:1]; %Anything you want for your res
y=[0:0.5:2]; %again, anything you want

Q=zeros(length(x),length(y)); %zeros matrix

for i=1:length(x)
for j=1:length(y)
Q(i,j) = dblquad(@integrand, 1, 2i, 0, 2*pi);
end
end
Q

now you have a matrix Q witch you can plot or do what ever you want with it.
Hope it helps.
* take into considaration that this are "for" loops so it may take some time.
I checked it on matlab 6.5 and it work good.
0

LVL 1

Expert Comment

*** 2 Correction:
1) ignore the comment: "replacing x<-> i....." on the 3rd row.
2) I mistakely wrote '2i' in the dblquad function so change it to 2.
0

LVL 27

Expert Comment

www.temp.qzxservices.com.
-
I thought that the mathCad solution would be simpler, but I am not very familiar with Matlab. Therefore I was quite interested in the Matlab solution. As you can see the MathCad solution is much simpler. I had not realized how much simpler.

The link goes to the solution with graph and includes the complete programing for the solution.
0

LVL 1

Expert Comment

True,
Your solution looks pretty simple. But, they are actually the same solutions only Matlab is a programming language and MathCad is a CAD program.
I used MathCad alot when started University but pretty soon I had to start using Mathematica and Matlab as they were much more powerful.
I recommend you try one of them. They may look very different at the begining but after some time you get used to them.
0

LVL 27

Expert Comment

Of course they are the same solution. If not, one would be wrong.
I am not sure what you mean by saying that MathCad is a CAD program while the others are a programing language. If you want, you can insert a section in a language like C++ into Mathcad. Mathcad started out as only a numberical solution program but it now incorportaes Maple for symbolic solutions.
Mathematica does have the (deserved) reputation of being the most powerful math program. Matlab is very popular and I will take your word that it is more powerful, especially if you make full use of its matrix orientation.
There is somethig to be said for simplicity and transparentcy, however (especially when coupled with a lot of power).
0

LVL 1

Expert Comment

I'll explain what I meant,
You said yourself: "you CAN insert...".
Now, in Matlab you don't have much choice since it is just a programing language. That might make some people prefer Mathematica or MathCad - which is still the most easy and simple math program I dealt with but was somewhat weak for advanced mathematical needs.
0

Author Comment

Thank you The_T

That was what I needed for my calculations

Tryso
0

## Featured Post

### Suggested Solutions

physics 211 7 37
Python tuple index error 4 58
Formatting Rounded Math Output in Visual Basic VS2015 4 27
Binomial distribution 2 43
Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…