In: Computer Science
(MATLAB) im trying to solve an equation for gravitational force for 41 radius values in between 3.8e8 to 4e8, heres a snippet of the code... how would a create this equation to solve for all 43 values (3.8e8 - additional 41 values in between- 4e8) the values are evenly spaced. (MATLAB)
r=(3.8e8:(2e7/42):4e8);
Mass_earth=5.97e24;
Mass_moon=7.34e22;
Force=((G*Mass_earth*Mass_moon)/(r^2));
fprintf(''),disp(Force(1))
The code to solve for all 43 values of 'r'
clc;clear;
r = (3.8e8:(2e7/42):4e8);
Mass_earth=5.97e24;
Mass_moon=7.34e22;
G = 9.8;
Force=((G*Mass_earth*Mass_moon)./(r.^2)); % added vector operators
./ and .^
fprintf(''),disp(Force)
Sample output of the code
Remarks
The variable 'r' is basically an array of 43x1 element. So to perform operations element wise operations of ^, *, /,.. we have to use the operators .^ ./ and .* in Matlab. Otherwise we have to use the loop. if loop is used then the last two lines of code can be rewritten as
for i = 1:43
Force=((G*Mass_earth*Mass_moon)/(r(i)^2)); % used / and ^
operators
fprintf(''),disp(Force);
end