I I'm new to matlab and Ikeep getting "Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in "THRUST CURVE FUNCTION LINE 3"

3 次查看(过去 30 天)
I I'm new to matlab and Ikeep getting "Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in "THRUST CURVE FUNCTION LINE 3"
function output_value = thrust_curve_function(t)
thrust_curve_values = readmatrix('thrust_curve_data.csv');
thrust_curve_t_values = thrust_curve_values(0.01 , 0.5 , 0.1 , 2 , 2.2 , 2.45 , 2.72);
thrust_curve_f_values = thrust_curve_values(50 , 56 , .48 , 24 , 19 , 5 , 0);
output_value = interp1(thrust_curve_t_values,thrust_curve_f_values,t);
end
  4 个评论
Ryan
Ryan 2023-10-23
forgot to post second part of code
start_t_value = 0;
end_t_value = 2.75;
integral@thrust_curve_function(thrust_curve_function), start_t_value, end_t_value;

请先登录,再进行评论。

回答(2 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023-10-23
From your post, what I understood is that you're trying to make linear interpolation of the imported data. I am not 100% sure if this is the case or not :). Anyhow, if this is what I guessed, here is a simple example code how to solve this exercise, e.g.:
FName = 'F_time.csv'; % This is a data file name. The data will be read from this file
t = 0:.1:2.75; % Input variable t values to be used for interp1()
% NOTE: Input and output variable names can be altered
[thrust_curve_values, output_value] = thrust_curve_function(t, FName);
plot(thrust_curve_values(:,1), thrust_curve_values(:,2), 'ro'), hold on
plot(t, output_value, 'k-')
legend('thrust_curve_values', 'inperpolated values')
xlabel('t')
ylabel('Output')
grid on
function [thrust_curve_values, output_value] = thrust_curve_function(t, FName)
thrust_curve_values = readmatrix(FName);
t_values = thrust_curve_values(:,1); % Column 1 of the imported data is t
F_values = thrust_curve_values(:,2); % Column 2 of the imported data is F(t)
output_value = interp1(t_values,F_values,t);
end
All the best
  2 个评论
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023-10-24
Your error on Line 3 is linked with the index values that must be integers, e.g. 1, 2, 3, ...
Here is a demo example with error on Line 3:
t = 0:.1:2.75; % Input variable t values to be used for interp1()
[thrust_curve_values, output_value] = thrust_curve_function(t)
function [thrust_curve_values, output_value] = thrust_curve_function(t)
thrust_curve_values = readmatrix('DATA_file.csv');
t_values = thrust_curve_values(0.01 , 0.5 , 0.1 , 2 , 2.2 , 2.45 , 2.72); % Index related error
F_values = thrust_curve_values(:,2); % Column 2 of the imported data is F(t)
output_value = interp1(t_values,F_values,t);
end
Here is a demo example with the fixed index related error on Line 3, etc. Note that we are finding the indexes by using logical index operations - see IDX1 and IDX2
t = 0:.1:2.75; % Input variable t values to be used for interp1()
[thrust_curve_values, output_value] = thrust_curve_function(t)
function [thrust_curve_values, output_value] = thrust_curve_function(t)
thrust_curve_values = readmatrix('DATA_file.csv');
% Note here how indexes are found with respect to the logic condition values.
% % Index related error fixed
IDX1=find(thrust_curve_values(:,1)==0.01 | thrust_curve_values(:,1)==0.5...
| thrust_curve_values(:,1)==0.1 | thrust_curve_values(:,1)==2 | thrust_curve_values(:,1)==2.2 ...
|thrust_curve_values(:,1)==2.45 | thrust_curve_values(:,1)==2.72);
% See the values of IDX1
disp(IDX1)
t_values = thrust_curve_values(IDX1,1); % Index related error fixed
% Note here how indexes are found with respect to the logic condition values.
% % Index related error fixed
IDX2=find(thrust_curve_values(:,2)==50 | thrust_curve_values(:,2)==56 ...
| thrust_curve_values(:,2)==48 | thrust_curve_values(:,2)==24 | thrust_curve_values(:,1)==19 ...
|thrust_curve_values(:,2)==5 | thrust_curve_values(:,2)==0); % Index related error fixed
F_values = thrust_curve_values(IDX2,2); % Column 2 of the imported data is F(t)
output_value = interp1(t_values,F_values,t);
end
Note that IDX1 need to be sufficient for your planned data take out. Presumable t vs. F(t) from your imported data. because t is independent variable and F(t) is a dependent. Unless you have some special conditions for F(t).

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2023-10-24
You should not be using integral for that kind of situation.
You have a table of numeric values, and you are trying to integrate that table. Unless you have an additional thrust model then the best you can justify is to use trapz or cumtrapz
In order to go beyond that you would need to have a function that models the transition between the points.
For example you might choose to interpolate a polynomial of degree 6 over your 7 points, and then proceed to integrate that polynomial of degree 6. This would probably not be a good idea, but it is an example of one way to model the data. Another way would be to use a cubic spline interpolation.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by