How can I call variables numbers from a table using individual variable row and column
9 次查看(过去 30 天)
显示 更早的评论
clc
% Composition of sand (A, B, C, D, E)
A = 36.8;
B = 4.03;
C = 29.8;
D = 0.37;
E = 0.06;
% Total sand composition = sand_comp = [A B C D E ]
sand_comp = [36.8 4.03 29.8 0.37 0.06];
% To find the maximum value of y,
y=(sand_comp(2)*12)/(sand_comp(1)*1)
% I have different values of A B C D E in a table (attached csv) and would like to call for instance A11, B11, C11, D11, E11 from the table directly to find the combination of values that would give the maximum value of y. That is to continue by calling A21, B21, C21, D21, E21 until the last row and column without stopping until all the values in the table have been tried. Then I can clearly see the max 'y'.
采纳的回答
Voss
2022-2-18
M = readmatrix('input.csv')
Or, if your version of MATLAB is a little older:
M = csvread('input.csv')
Then the cells in the table correspond to elements of the matrix M, which can be accessed by indexing. E.g., get the element from row 7 column 5:
M(7,5)
Get all of row 2:
M(2,:)
Get rows 2, 5 and 8:
M([2 5 8],:)
Get columns 1 and 9:
M(:,[1 9])
And so on.
It's not clear to me how exactly your calculation will work though because you mention A11, ..., A21, etc., but this table has 10 rows and 9 columns.
5 个评论
Voss
2022-2-26
编辑:Voss
2022-2-26
It looks like you have one function called soil_2_crop_nut (which takes an input argument x) nested inside another function called soil_2_crop_nut (which takes no inputs). The nested function is called inside the outer function (in fsolve), but the outer function is never called.
I think you should call the outer function inside the for loop at the top, immediately after setting sand_nutrient and rain_water_steam. And you may have to change the name of one of the functions to get it to run properly (probably a good idea anyway). And the outer function's inputs and outputs will have to be set up differently.
I'll be able to look at it more closely in a few hours.
Voss
2022-2-26
Now it runs (see below) with those changes I mentioned. The solver gives a warning, so you may want to consider the warning message.
Now that the program is (or may be) set up correctly, if you have a question about the results you get or the usage of fsolve(), you may want to post a new question and ask that there, because there are people who know more about that than I do, and they may not see it here.
clearvars
clc
M = readmatrix('input_2.csv');
vol_water = zeros(size(M,1),6);
for ii = 1:size(M,1)
% sand_nutrient = [A B C D E];
sand_nutrient = M(ii,2:6);
rain_water_steam = M(ii,1);
vol_water(ii,:) = soil_2_crop_nut(sand_nutrient,rain_water_steam).';
end
disp(vol_water);
function vol_of_water_per_crop = soil_2_crop_nut(sand_nutrient,rain_water_steam)
%%
mole_of_sand=24.7;
%%
constant=0.27;
w=(mole_of_sand*rain_water_steam)./(18*(1-rain_water_steam))
%%
x=1;
y=(sand_nutrient(2)*12)/(sand_nutrient(1)*1);
z=(sand_nutrient(3)*12)/(sand_nutrient(1)*16);
%%
x0=[0.5 0.5 0.5 0.5 0.5 1000]'; % Guess
%%
options = optimset('Display','iter','Algorithm','trust-region-reflective','MaxFunEvals',10000);
x=fsolve(@soil_2_crop_nut,x0,options);
vol=x(1:5).*1000*8.314*x(6)./101325;
n2_out=2*3.76*constant;
vol_n2=n2_out*1000*8.314*x(6)./101325;
total_vol=(sum(vol)+vol_n2);
vol_of_water_per_crop=100*vol./total_vol;
vol_per_n2=100*vol_n2./total_vol;
vol_of_water_per_crop=[vol_of_water_per_crop; vol_per_n2];
fprintf('crop A %f\n',vol_of_water_per_crop(1))
fprintf('crop B %f\n',vol_of_water_per_crop(2))
fprintf('crop C %f\n',vol_of_water_per_crop(3))
fprintf('crop D %f\n',vol_of_water_per_crop(4))
fprintf('crop E %f\n',vol_of_water_per_crop(5))
fprintf('crop F %f\n',vol_of_water_per_crop(6))
function f=soil_2_crop_nut(x) % Function F(x) to solve for x1-x6.
f_1=1-(x(1)+x(2)+x(3));
f_2=(y + 2*w)-(4*x(3) +2*x(4) +2*x(5));
f_3=(z+w +2*constant)-(x(1) +2*x(2) +x(5));
f=[f_1;f_2;f_3];
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!