Indexing a variable value

2 Ansichten (letzte 30 Tage)
Robert Burke
Robert Burke am 3 Dez. 2018
Kommentiert: Aoi Midori am 3 Dez. 2018
Hi,
I have been given an assignment where we basically have to run a simulation for a rocket launch. In part of this, we have to calculate the density of the atmosphere at the height of the rocket so that we can calculate the drag force. The heights of the rocket (in increments of one kilometre) have been given to us in column one of an excel sheet, with their respective density values alongside them in column 2 (stored in a file called DensityData) I have already read in these values using xlsxread.
To simulate the launch, we have used a for loop for time. However, I am trying to write a subscript for a function that will produce the density value for each iteration of the for loop. I attempted to write the function as
function [density] = findDensity(DensityData.m) %g/cm^3
%DensityData : returns the density of the atmosphere
%This uses the altitude of the rocket to produce a value for the density of
%the atmosphere at that height
[density] = DensityData(position,1);
end
but this produces an error as it won't let me enter a non-numerical or non-logical value for the row value in the index, i.e. position.
If anybody could help me find an alternative way around this, that would be much appreciated!
P.s. the heights are in increments of 1 km so a round function may also have to be used on the position value before using it for any code similar to the above.
Regards, Rob

Antworten (3)

Aoi Midori
Aoi Midori am 3 Dez. 2018
Do you want to calculate the density values using the function DensityData() or do you already have the density values calculated and the matrix DensityData where it contains the heights in column 1 and the density values in column 2? In the latter case, this could be one of the solutions.
function [density] = findDensity(DensityData, position)
[density] = DensityData(position,2);
end
  3 Kommentare
Dennis
Dennis am 3 Dez. 2018
How do you call the function? Why do you need in in the first place if you already have heights and densities in a matrix?
Aoi Midori
Aoi Midori am 3 Dez. 2018
I was also wondering for a while and came up with the idea that Robert just wanted to look up the density values using the row number 'position'...

Melden Sie sich an, um zu kommentieren.


Robert Burke
Robert Burke am 3 Dez. 2018
I wish I could do that but the assignment specifies that we create a user-defined function in a subscript and then use that in the main script.
The function I used was the one Aoi sent above, and in the main script I wrote
[density] = findDensity(DensityData,position)
where findDensity is the name of the function we were told to use
  1 Kommentar
Aoi Midori
Aoi Midori am 3 Dez. 2018
I would highly appreciate it if you could attach the screen capture and DensityData, if you still have some difficulties. Thank you in advance.

Melden Sie sich an, um zu kommentieren.


Dennis
Dennis am 3 Dez. 2018
function density = FindDensity(DataDensity,position)
[~,idx]=min(abs(DataDensity(:,1)-position));
density=DataDensity(idx,2);
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by