Integrating Equations in MATLAB: Tips and Tricks

  • Thread starter dorikin
  • Start date
  • Tags
    Matlab
In summary, the conversation is about a person seeking help in writing out an equation in MATLAB for graphing. They are specifically stuck on the integration part and are unsure if they need to use the ode45 or quadl function. They have attached a picture of the equation and code they have written so far. Another person suggests simplifying the problem and using the quad function to calculate the integral, then using a loop to repeat this process for a range of values. They also mention the use of the int() function for symbolic integration. The conversation ends with a humorous side note about pronunciations.
  • #1
dorikin
7
0
Hello everyone!

I'm trying to write out an equation in MATLAB from a book so that I can make a graph. I am stuck on how to write the intergration part of the equation.

I have uploaded a picture of the page here: http://www.flickr.com/photos/61865210@N07/5736930748/

I am trying to write equation 4.19

The code I have written so far is below. Do I have to use ode45 or quadl? I've looked in the help file but I don't understand >.<

If you have any ideas please post them, I've been trying to do this for the last day! >.<

Thank you!

ken



Homework Statement


Homework Equations


It will probably be more clear in the link


The Attempt at a Solution



clear all
clc

[x,y] = meshgrid(-1:.5:1);

L=0.5;

constant=-1/(4*pi);

ln1=log(x.^2+(y-l)^2); %problem

soln= quadl(ln1,-L,L); %problem

potential=constant*soln;

mesh(x,y,potential)
 
Physics news on Phys.org
  • #2
Hello!

I think I have a solution.

I will post it up once I have checked it1

Thanks

ken
 
  • #3
Minor point, but there is no such word as "intergrate."

The one you want is integrate.
 
  • #4
Thanks for letting me know, I thought it looked a bit weird *red cheeks*

Here is the solution:

"I would start by simplifying the problem a bit.

Step 1 - for a fixed x1,x2, how could I calculate the integral?

For this, you can actually use the quad function (you don't need ode45). Let's assume x1 = 7 and x2 = 3. I could create a function that would return the value of the integrand for any particular L

integrand = @(L) log(7.^2+(3-L).^2);
integrand(14)

I can then use this function to do the integral numerically

quad(integrand,-5,5)

So, we've solve step 1! :)

Step 2 - Repeat process for a bunch of x1,x2 combos

Well, this is fairly straightforward now that we solved step 1. We just use a loop.

lambda = 1;
x1 = 1:0.1:2;
x2 = 1:0.1:2;
[X1,X2] = meshgrid(x1,x2);
phi = zeros(size(X1));
for i = 1:numel(X1)
fixedx1x2 = @(L) log(X1(i).^2+(X2(i)-L +eps).^2);
phi(i) = (lambda / 4*pi) * quad(fixedx1x2,-5,5);
end
surf(X1,X2,phi)

Note that here we are using meshgrid to calculate a range of x1,x2 values over a grid, and then for each x1 and x2 we are repeating the quad call

Hope this helps!"

Thanks to anyone who tried to help me out!

ken
 
  • #5
Mark44 said:
Minor point, but there is no such word as "intergrate."

The one you want is integrate.
There is if you have a Boston accent! My Caculus I teacher talked about "delters" and "alphers".
 
  • #6
If you want to integrate symbolically, you can use the int() function on a sym object.
 
  • #7
After you have done symbolic integration, you can't sub in numbers can you?

I've tired it and it doesn't seem to work but I want to double check.

thanks ken
 

Related to Integrating Equations in MATLAB: Tips and Tricks

1. How do I integrate a function in Matlab?

To integrate a function in Matlab, you can use the integral function. This function takes in the function you want to integrate, the lower and upper limits, and any optional parameters. It returns the numerical value of the integral.

2. Can I integrate multiple functions at once in Matlab?

Yes, you can integrate multiple functions at once in Matlab by using the arrayfun function. This function allows you to apply a function to each element of an array, in this case, the array of functions you want to integrate.

3. How do I specify the integration method in Matlab?

By default, Matlab uses the adaptive Simpson's rule to integrate a function. However, you can specify a different integration method by using the 'method' parameter in the integral function. Some other available methods include trapezoidal rule, Gaussian quadrature, and Romberg integration.

4. Is it possible to integrate over a specific range in Matlab?

Yes, you can integrate over a specific range in Matlab by using the 'ArrayValued' parameter in the integral function. This parameter allows you to specify the range as an array.

5. How can I improve the accuracy of my integration in Matlab?

To improve the accuracy of your integration in Matlab, you can decrease the 'AbsTol' and 'RelTol' parameters in the integral function. These parameters control the absolute and relative error tolerances, respectively. However, decreasing these values may also increase the computation time.

Similar threads

  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
24
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Calculus and Beyond Homework Help
Replies
7
Views
886
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top