I want to run a list of distance values through these two formulas for force and combine them into one 1x3 matrix in the form [dist force1 force2].]

조회 수: 1 (최근 30일)
clear;
clc
mmass=input('What is the mass of the main object?');
sat1=input('What is the mass of the first satellite?');
sat2=input('What is the mass of the second satellite?');
for DistVector = 200e6:10e6:100e7;
Force1=(mmass*(sat1^2))/(DistVector.^2);
Force2=(mmass*(sat2^2))/(DistVector.^2);
your_matrix = [DistVector(:), Force1(:), Force2(:)]
end

답변 (1개)

Jon
Jon 2021년 10월 26일
편집: Jon 2021년 10월 26일
You shouldn't need to use a loop for that. MATLAB can calculate the formulas directly as vectors for all of the points in DistVector. Then just concatenate the results columnwise in your output array
So something like this. You can check the formulas to make sure they are correct. I removed a bunch of unneeded parentheses, and also note the ./ for element by element division of the scalar numerator and the vector denominator
mmass = 10;
sat1 = 5;
sat2 = 3;
DistVector = 200e6:10e6:100e7
Force1=mmass*sat1^2 ./DistVector.^2;
Force2=mmass*sat2^2 ./DistVector.^2;
your_matrix = [DistVector(:), Force1(:), Force2(:)]
It looks like you are calculating the gravitational attraction between a satellite and main mass. If so, I wonder if your formula is correct. Shouldn't it just be proportional to m1*m2/d^2 ? You have m1*m2^2/d^2, so you have units of mass cubed in the numerator. Also shouldn't there be a constant of proportionality, G, in there?

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by