How can I call variables numbers from a table using individual variable row and column

9 Ansichten (letzte 30 Tage)
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'.

Akzeptierte Antwort

Voss
Voss am 18 Feb. 2022
M = readmatrix('input.csv')
M = 10×9
10.9000 2.9400 69.1100 17.2100 44.9100 5.0300 35.7000 0.4900 0.0300 10.9000 2.0500 69.5900 17.4600 49.0900 5.6100 31.9100 0.3900 0.0400 10.7000 4.9400 62.1400 12.5400 45.4000 4.5200 34.1300 0.3100 0.0010 9.4000 2.7200 69.8700 17.2300 46.7500 5.4400 35.6100 0.0010 0.0200 9.8000 2.5300 68.1700 16.3800 45.3700 5.3200 35.0100 0.3800 0.0330 12.3000 3.5000 70.3600 13.8400 44.1600 5.1100 34.7500 0.1000 0.0600 20.4000 6.8000 59.3200 13.4800 37.4600 4.5700 30.3700 0.3100 0.0900 16.9200 7.7800 62.8500 12.4500 39.2700 4.3200 31.3000 0.3300 0.0400 20.4600 8.4600 58.5100 12.5800 36.8000 4.0300 29.8000 0.3700 0.0600 25.1100 8.3900 54.4200 11.5800 34.6700 4.0400 27.4000 0.3500 0.0100
Or, if your version of MATLAB is a little older:
M = csvread('input.csv')
M = 10×9
10.9000 2.9400 69.1100 17.2100 44.9100 5.0300 35.7000 0.4900 0.0300 10.9000 2.0500 69.5900 17.4600 49.0900 5.6100 31.9100 0.3900 0.0400 10.7000 4.9400 62.1400 12.5400 45.4000 4.5200 34.1300 0.3100 0.0010 9.4000 2.7200 69.8700 17.2300 46.7500 5.4400 35.6100 0.0010 0.0200 9.8000 2.5300 68.1700 16.3800 45.3700 5.3200 35.0100 0.3800 0.0330 12.3000 3.5000 70.3600 13.8400 44.1600 5.1100 34.7500 0.1000 0.0600 20.4000 6.8000 59.3200 13.4800 37.4600 4.5700 30.3700 0.3100 0.0900 16.9200 7.7800 62.8500 12.4500 39.2700 4.3200 31.3000 0.3300 0.0400 20.4600 8.4600 58.5100 12.5800 36.8000 4.0300 29.8000 0.3700 0.0600 25.1100 8.3900 54.4200 11.5800 34.6700 4.0400 27.4000 0.3500 0.0100
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)
ans = 37.4600
Get all of row 2:
M(2,:)
ans = 1×9
10.9000 2.0500 69.5900 17.4600 49.0900 5.6100 31.9100 0.3900 0.0400
Get rows 2, 5 and 8:
M([2 5 8],:)
ans = 3×9
10.9000 2.0500 69.5900 17.4600 49.0900 5.6100 31.9100 0.3900 0.0400 9.8000 2.5300 68.1700 16.3800 45.3700 5.3200 35.0100 0.3800 0.0330 16.9200 7.7800 62.8500 12.4500 39.2700 4.3200 31.3000 0.3300 0.0400
Get columns 1 and 9:
M(:,[1 9])
ans = 10×2
10.9000 0.0300 10.9000 0.0400 10.7000 0.0010 9.4000 0.0200 9.8000 0.0330 12.3000 0.0600 20.4000 0.0900 16.9200 0.0400 20.4600 0.0600 25.1100 0.0100
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 Kommentare
Voss
Voss am 26 Feb. 2022
Bearbeitet: Voss am 26 Feb. 2022
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
Voss am 26 Feb. 2022
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
w = 0.1679
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.11784 9.78 0.01 1 14 3.88303e-06 0.00338 0.001 0.52821 2 21 3.44801e-12 1.4e-06 0.0001 0.00176405 3 28 4.41143e-20 1.46e-10 1e-05 1.97401e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.504050 crop B 9.660550 crop C 5.532054 crop D 8.440579 crop E 4.597079 crop F 58.265687
w = 0.1679
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.15479 9.67 0.01 1 14 6.08852e-06 0.00381 0.001 0.553261 2 21 6.07902e-12 1.83e-06 0.0001 0.00233561 3 28 7.77968e-20 1.94e-10 1e-05 2.62143e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.795101 crop B 8.792899 crop C 6.512569 crop D 8.407918 crop E 3.405717 crop F 59.085796
w = 0.1644
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.91825 10.4 0.01 1 14 5.40768e-06 0.00378 0.001 0.568112 2 21 5.23979e-12 1.7e-06 0.0001 0.00216771 3 28 6.70784e-20 1.8e-10 1e-05 2.43411e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 14.146884 crop B 9.684606 crop C 5.371542 crop D 7.982706 crop E 3.520427 crop F 59.293836
w = 0.1424
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.18429 9.78 0.01 1 14 4.88725e-06 0.0036 0.001 0.541236 2 21 4.64446e-12 1.61e-06 0.0001 0.00204373 3 28 5.9433e-20 1.69e-10 1e-05 2.29125e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.675281 crop B 9.281870 crop C 5.950938 crop D 8.395168 crop E 4.001757 crop F 58.694986
w = 0.1491
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.05184 9.68 0.01 1 14 4.4688e-06 0.0035 0.001 0.532709 2 21 4.14875e-12 1.52e-06 0.0001 0.0019331 3 28 5.30825e-20 1.6e-10 1e-05 2.16539e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.546583 crop B 9.343872 crop C 5.902973 crop D 8.473554 crop E 4.270843 crop F 58.462176
w = 0.1925
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 5.66613 9.41 0.01 1 14 3.1597e-06 0.00316 0.001 0.505611 2 21 2.6082e-12 1.23e-06 0.0001 0.00153882 3 28 3.33502e-20 1.27e-10 1e-05 1.71642e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.138727 crop B 9.625098 crop C 5.649385 crop D 8.705119 crop E 5.191490 crop F 57.690181
w = 0.3517
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 3.85902 7.83 0.01 1 14 7.56249e-07 0.00301 0.001 0.402052 2 21 4.1177e-15 1.37e-07 0.0001 0.000214504 Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance and the vector of function values is near zero as measured by the value of the function tolerance. crop A 11.383315 crop B 10.173891 crop C 5.351066 crop D 9.833299 crop E 8.623875 crop F 54.634554
w = 0.2795
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 5.08814 8.98 0.01 1 14 1.59932e-06 0.00314 0.001 0.465562 2 21 8.1782e-13 7.24e-07 0.0001 0.000879675 Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance and the vector of function values is near zero as measured by the value of the function tolerance. crop A 12.497057 crop B 10.115810 crop C 5.192547 crop D 9.059839 crop E 6.678593 crop F 56.456155
w = 0.3530
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 4.41977 8.42 0.01 1 14 8.49459e-07 0.00318 0.001 0.427352 2 21 4.73934e-14 2.36e-07 0.0001 0.000289312 Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance and the vector of function values is near zero as measured by the value of the function tolerance. crop A 11.812279 crop B 10.441664 crop C 4.942193 crop D 9.477719 crop E 8.107103 crop F 55.219042
w = 0.4601
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 3.24318 7.23 0.01 1 14 1.06286e-06 0.00307 0.001 0.371997 2 21 5.80824e-13 4.46e-07 0.0001 0.000738524 Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance and the vector of function values is near zero as measured by the value of the function tolerance. crop A 10.687499 crop B 10.537437 crop C 5.057788 crop D 10.251466 crop E 10.101403 crop F 53.364406
w = 0.1610
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.40468 10 0.01 1 14 4.37408e-06 0.00352 0.001 0.542324 2 21 4.02185e-12 1.5e-06 0.0001 0.00190265 3 28 5.14693e-20 1.58e-10 1e-05 2.13221e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.732249 crop B 9.683000 crop C 5.458399 crop D 8.275274 crop E 4.226024 crop F 58.625054
w = 0.1525
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.47873 10.1 0.01 1 14 4.58842e-06 0.00357 0.001 0.546818 2 21 4.27546e-12 1.54e-06 0.0001 0.00196089 3 28 5.47186e-20 1.63e-10 1e-05 2.19847e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.802000 crop B 9.653297 crop C 5.479719 crop D 8.232016 crop E 4.083312 crop F 58.749656
w = 0.0912
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 7.09277 10.4 0.01 1 14 7.48863e-06 0.00414 0.001 0.593024 2 21 7.7484e-12 2.05e-06 0.0001 0.00263213 3 28 9.9206e-20 2.19e-10 1e-05 2.96016e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 14.476708 crop B 9.087860 crop C 6.019356 crop D 7.868862 crop E 2.480014 crop F 60.067199
w = 0.0722
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 7.67747 10.9 0.01 1 14 9.44368e-06 0.00448 0.001 0.626115 2 21 1.01019e-11 2.33e-06 0.0001 0.00300224 3 28 1.29363e-19 2.5e-10 1e-05 3.38024e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 14.989040 crop B 8.946473 crop C 6.083907 crop D 7.535860 crop E 1.493293 crop F 60.951428
w = 0.0722
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 7.55788 10.8 0.01 1 14 9.28617e-06 0.00445 0.001 0.621665 2 21 9.91377e-12 2.31e-06 0.0001 0.00297451 3 28 1.2695e-19 2.47e-10 1e-05 3.34858e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 14.908838 crop B 8.898629 crop C 6.157689 crop D 7.601899 crop E 1.591690 crop F 60.841254
w = 0.2653
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 5.05818 8.89 0.01 1 14 2.1571e-06 0.00296 0.001 0.472547 2 21 1.45828e-12 9.39e-07 0.0001 0.00116087 3 28 1.86152e-20 9.51e-11 1e-05 1.28246e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 12.606844 crop B 9.668256 crop C 5.706596 crop D 9.071429 crop E 6.132841 crop F 56.814034
w = 0.2465
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 5.68641 9.38 0.01 1 14 3.74985e-06 0.0033 0.001 0.513322 2 21 3.30446e-12 1.37e-06 0.0001 0.00172881 3 28 4.22625e-20 1.43e-10 1e-05 1.93218e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.238296 crop B 9.350332 crop C 5.958377 crop D 8.689560 crop E 4.801596 crop F 57.961839
w = 0.2486
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 5.74947 9.48 0.01 1 14 3.26234e-06 0.0032 0.001 0.509506 2 21 2.72624e-12 1.25e-06 0.0001 0.00157237 3 28 3.48636e-20 1.3e-10 1e-05 1.75492e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 13.202667 crop B 9.644891 crop C 5.612560 crop D 8.656117 crop E 5.098342 crop F 57.785423
w = 0.1525
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 7 6.78841 10.2 0.01 1 14 6.2108e-06 0.0039 0.001 0.572311 2 21 6.21507e-12 1.84e-06 0.0001 0.00235962 3 28 7.95622e-20 1.96e-10 1e-05 2.65096e-06 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. crop A 14.171019 crop B 9.279470 crop C 5.852271 crop D 8.046232 crop E 3.154683 crop F 59.496324
disp(vol_water);
13.5040 9.6605 5.5321 8.4406 4.5971 58.2657 13.7951 8.7929 6.5126 8.4079 3.4057 59.0858 14.1469 9.6846 5.3715 7.9827 3.5204 59.2938 13.6753 9.2819 5.9509 8.3952 4.0018 58.6950 13.5466 9.3439 5.9030 8.4736 4.2708 58.4622 13.1387 9.6251 5.6494 8.7051 5.1915 57.6902 11.3833 10.1739 5.3511 9.8333 8.6239 54.6346 12.4971 10.1158 5.1925 9.0598 6.6786 56.4562 11.8123 10.4417 4.9422 9.4777 8.1071 55.2190 10.6875 10.5374 5.0578 10.2515 10.1014 53.3644 13.7322 9.6830 5.4584 8.2753 4.2260 58.6251 13.8020 9.6533 5.4797 8.2320 4.0833 58.7497 14.4767 9.0879 6.0194 7.8689 2.4800 60.0672 14.9890 8.9465 6.0839 7.5359 1.4933 60.9514 14.9088 8.8986 6.1577 7.6019 1.5917 60.8413 12.6068 9.6683 5.7066 9.0714 6.1328 56.8140 13.2383 9.3503 5.9584 8.6896 4.8016 57.9618 13.2027 9.6449 5.6126 8.6561 5.0983 57.7854 14.1710 9.2795 5.8523 8.0462 3.1547 59.4963
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by