Mass calculation by looping tabulated values from a table

Hello,
I woking with a script that computes the mass of earth from the integral in figure 1 and tabulated density from table 1. I getting the answer 5.8589e+17 while running this (correct answer is 5.9724e+24). Is there any problem with my if-loop?
Figure 1.
Table 1.
clear all; close all;
R = 6370000; % Outer radius of earth [m]
N = 100000; % Number of random numbers used
Nsphere = 0; % Initiate Nsphere
fsum = 0; % Initiate fsum
D=Nsphere/(N*8*R.^3); % Domain
for i = 1:N
x = -R+2*R*rand; % x is assigned a random number between -R and R
y = -R+2*R*rand; % y is assigned a random number between -R and R
z = -R+2*R*rand; % z is assigned a random number between -R and R
r = x^2 + y^2 + z^2; % Distance squared of the point (x,y,z) from origo
%Inner core
if r <= 1200000^2
d1=14000; %Density
Nsphere = Nsphere + 1; % Increment Nsphere
fsum = fsum + x.^2 + y.^2; % Sums up the function value
I1 = size(D)*(fsum/Nsphere); % Integral sum (volume)
M1=I1(1,1)*d1; % Mass inte region
end
% Outer core
if (r > 1200000^2) && (r <= 3460000^2)
d2=11000; %Density
Nsphere = Nsphere + 1; % Increment Nsphere
fsum = fsum + x.^2 + y.^2; % Sums up the function value
I2 = size(D)*(fsum/Nsphere); % Integral sum (volume)
M2=I2(1,1)*d2; % Mass inte region
end
% Lower mantle
if (r > 3460000^2) && (r <= 5630000^2)
d3=4850; %Density
Nsphere = Nsphere + 1; % Increment Nsphere
fsum = fsum + x.^2 + y.^2; % Sums up the function value
I3 = size(D)*(fsum/Nsphere); % Integral sum (volume)
M3=I3(1,1)*d3; % Mass inte region
end
% Upper mantle
if (r > 5630000^2) && (r <= 6340000^2)
d4=3800; %Density
Nsphere = Nsphere + 1; % Increment Nsphere
fsum = fsum + x.^2 + y.^2; % Sums up the function value
I4 = size(D)*(fsum/Nsphere); % Integral sum (volume)
M4=I4(1,1)*d4; % Mass inte region
end
% Crust
if (r > 6340000^2) && (r <= 6370000^2)
d5=2600; %Density
Nsphere = Nsphere + 1; % Increment Nsphere
fsum = fsum + x.^2 + y.^2; % Sums up the function value
I5 = size(D)*(fsum/Nsphere); % Integral sum (volume)
M5=I5(1,1)*d5; % Mass inte region
end
end
Mass_tot_calculated=M1+M2+M3+M4+M5 % Mass calculated from the integral
Mass_wiki=5.97237*10^24 % Mass from Wikipedia [kg]

Answers (0)

Tags

Asked:

on 31 Mar 2019

Community Treasure Hunt

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

Start Hunting!