how do I write the intergration part of an equation into matlab

1 回表示 (過去 30 日間)
ken
ken 2011 年 5 月 19 日
Hello everyone!
I'm trying to write out an equation 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 wriiten so far is below. Do I have to use ode45? I've looked in the help file but I don't understand it >.<
Thank you for your help!
ken
clear all
clc
[x,y] = meshgrid(-1:.5:1);
%x=x+5;
L=0.5;
constant=-1/(4*pi);
ln1=log(x^2+(y-l)^2);
= ode(ln1)
ans=constant* ;
  1 件のコメント
ken
ken 2011 年 5 月 20 日
Can anyone help me please? I've been looking for examples online but can't find any which solve equations similar to mine.
Inputting the x1 and x2 is confusing me.
Thanks!
ken

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

採用された回答

Jarrod Rivituso
Jarrod Rivituso 2011 年 5 月 20 日
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).^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!
  1 件のコメント
ken
ken 2011 年 5 月 20 日
Thank you Jarrod!
I will use your way of thinking the next time I hit a problem!
It does exactly what I wanted! Thank you! Explained v. well too!
The only edit I had to make was to add +eps to the L in the fixedx1x2 function to make the graph better.
The "phi = zeros(size(X1));" is to preallocate space in the growing matrix right? I haven't done this before so another thing learnt!
Thanks again!
ken

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by