loop structure variables in formula
2 views (last 30 days)
Show older comments
I need help i have the following formula which runs as a function : f=G*(Mass1*Mass2)/((Distance1 - Distance2)^2) %<< this formula compares gravitational force between 2 planets.
G, Mass 1 and Distance 1 have already been defined in the script via user input (select planet name) these values are constant.
I need to extract Mass2 and Distance 2.
the following structure is where my data is extracted.

I need the formula to loop and keep changing Mass2 and Distance 2 so that it presents all the possibilities as 1 list. e.g
Note: venus has been selected as a user input which pre-defined Mass 1 and Distance1. So it cannot be presented in the list.
‘Sun’ [5.52599e+22] %< These values are not correct, just represents the formula result
‘Mercury’ [4.40086e+16]
‘Earth’ [1.10589e+18]
‘Mars’ [1.45682e+16]
‘Jupiter’ [1.37411e+18]
‘Saturn’ [1.06038e+17]
‘Uranus’ [3.71529e+15]
‘Neptune’ [1.72081e+15]
‘Pluto’ [1.20524e+11]
‘Mercury’ [4.40086e+16]
I need help making the loop and list work. If you can provide the code for this it would be much appreciated. Thanks
0 Comments
Accepted Answer
Stephen23
on 27 Jan 2019
Edited: Stephen23
on 28 Jan 2019
Rather than using loops you should learn how to use neat comma-separated lists:
and vectorized code:
and some simple logical indexing. For your data and formula, something like this:
% Fake data (replace with your own):
S = struct(...
'Name',{'Sun','Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune','Pluto'},...
'Mass',{1989000,0.33,4.87,5.97,0.642,1899,568,86.8,102,0.0125},...
'Dist',{0,0.39,0.723,1,1.523,5.203,9.539,19.18,30.06,39.53});
% Code:
U = 'venus'; % user selection.
X = strcmpi(U,{S.Name});
D2 = [S(~X).Dist];
M2 = [S(~X).Mass];
D1 = S(X).Dist;
M1 = S(X).Mass;
G = 6.673e-11; % change to suit your units
F = G*(M1*M2)./((D1-D2).^2);
C = [{S(~X).Name};num2cell(F)];
fprintf('%-11s %g\n',C{:})
Which prints (note that you will need to provide the correct gravitational constant value for G):
Sun 0.00123654
Mercury 9.67109e-10
Earth 2.52851e-08
Mars 3.25991e-10
Jupiter 3.07482e-08
Saturn 2.37495e-09
Uranus 8.28033e-11
Neptune 3.8514e-11
Pluto 2.69737e-15
2 Comments
Stephen23
on 28 Jan 2019
Alias:Max's "Answer" moved here:
Definetly works.
Your coding method is definetly more realistic for this scenario. My limitation is that i need to call a struct of data rather than create my own. In the case that i import an excel sheet with lots of data and turn it into a struct. The data sheet could be far to big to build a struct using the method above.
But your response has deffinetly helped for my current situation, i will use your method.
Thanks for all your help.
If you are interested, i can provide you with a copy of my data sheets and full challenge brief, to see how your coding as a professional compares with an amateur.?
Stephen23
on 28 Jan 2019
Edited: Stephen23
on 28 Jan 2019
@Alias:Max: please do not write comments as answers. Please do not accept your own comment as the accepted answer. Answers are for answering the question, which your comment does not. Comments are for commenting on a question or answer. Accepting an answer shows that you consider that that answer resolves your question (which apparently you do).
"My limitation is that i need to call a struct of data rather than create my own. "
Why is that a "limitation"? You wrote that you already have that structure (and you showed a screenshot of it), so it is not clear what the "limitation" is. I just created a fake data structure because you did not upload any actual data for us to use: if you do not upload actual data then we have to create fake data to test code on (screenshots are not data: we cannot import them, we cannot load them, we cannot edit them, we cannot search them... )
"But your response has deffinetly helped for my current situation, i will use your method."
Then you can show your appreciation by accepting my answer, and also that of your earlier question:
"i can provide you with a copy of my data sheets and full challenge brief, to see how your coding as a professional compares with an amateur."
You are welcome to make a comment and upload your data by clicking the paperclip button.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!