How would I manually perform an integral in MATLAB?
6 ビュー (過去 30 日間)
古いコメントを表示
My professor has told us not to use the integral3 function in matlab to solve a triple integral. He explained to me today that I should use a nested loop in order to do what we normally would do with an integral. I can't seem to figure out how to do this in matlab. Below I've provided the integral in question that I'm trying to solve as well as my code that I have currently. When I run the code below I get back 18y+54z+6 when the answer should be 16.
∭∇·F dV where dV is dx*dy*dz and the bounds for all three are from -1 to 1.
syms x y z
f = 2*x+y^2+z^2;
fx = diff(f,x)
fy = diff(f,y)
fz = diff(f,z)
F = fx+fy+fz
sum = 0;
dx = 1;
for x = -1:dx:1
sum = sum + fx*dx
for y = -1:dx:1
sum = sum + fy*dx
for z = -1:dx:1
sum = sum+fz*dx
end
end
end
Thank you for any help you might be able to provide!
2 件のコメント
Walter Roberson
2016 年 2 月 6 日
Please do not name any variable "sum". You interfere with the use of the important MATLAB function "sum" when you do that, and even if you are consistent about it in your code you are going to confuse other people who read the code. Remember, your assignment will be marked so other people need to read your code, and if they find it confusing you aren't going to get the best marks.
回答 (1 件)
Mehdi M
2016 年 2 月 5 日
Hi William, I think the function F should probably be like this. F= (2*x) i + (y^2) j + (z^2) k; So ∇·F= 2+2y+2z. now we integrate numerically, that is
clc
clear
h=.005;
dx=h;
dy=h;
dz=h;
x=-1:dx:1;
y=-1:dy:1;
z=-1:dz:1;
dv=dx*dy*dz;
m=0;
I=0;
for i=1:length(x)
for j=1:length(y)
for k=1:length(z)
m=m+1;
I=I+(2+2*y(j)+2*z(k))*dv;
end
end
end
I
To gain more accurate results you can decrease the value of "h"!
1 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!