# how to get variable of function in for loop

조회 수: 58 (최근 30일)
Amron Rosyada 2024년 5월 29일 3:15
답변: Steven Lord 2024년 5월 29일 13:26
I created a polynomial function f(x,y) using two variables. I varied the values of both variables to get the maximum f(x,y) using for loop and i got it. But, i dont know how to get or display the values of the x and y that made it. can you tell me the syntax to get them? thanks!!
x = 1:20;
y = 1:13;
f = [];
for i = 1:length(x), j = 1:length(j);
f(i,j) = x(i).^2 - x(i) -2*y(j).^2 + y(j) -25;
end
fmax = max(max(f));
display(fmax)
fmax = 354
##### 댓글 수: 2없음 표시없음 숨기기
VBBV 2024년 5월 29일 3:24
Do you mean display x and y values only or f values ?
x = 1:20 % delete the semi colon
x = 1x20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = 1:13 %
y = 1x13
1 2 3 4 5 6 7 8 9 10 11 12 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
disp(x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
disp(y)
1 2 3 4 5 6 7 8 9 10 11 12 13
Amron Rosyada 2024년 5월 29일 3:30
편집: Amron Rosyada 2024년 5월 29일 3:33
you didnt get my question. i got the fmax. but at which variable of x and y that excactly subtituted in that f function. i mean, when (the value of x and y subtituted) fmax happens?

댓글을 달려면 로그인하십시오.

### 답변 (2개)

Steven Lord 2024년 5월 29일 13:26
Don't call max twice. Call max once with two output arguments and specify 'all' as the dimension over which to operate. That second output will be the linear index of the location where the maximum value returned as the first output was located. Then use ind2sub to get the row and column indices.
x = 1:20;
y = 1:13;
I've written your function as a function handle so I can call it from multiple places without retyping it.
fh = @(x, y) x.^2-x-2*y.^2+y-25;
Note that as originally written, this for loop doesn't do what you think it does. You can't have one for loop iterate over two arrays like this.
% f = [];
% for i = 1:length(x), j = 1:length(j);
% f(i,j) = fh(x(i), y(j));
% end
You need two for loops. Or you could use the vectorized approach others have suggested. But there's no need to grid the data explicitly as others have done; you can take advantage of the implicit expansion behavior of the basic operators in MATLAB like plus.
f = fh(x, y.')
f = 13x20
-26 -24 -20 -14 -6 4 16 30 46 64 84 106 130 156 184 214 246 280 316 354 -31 -29 -25 -19 -11 -1 11 25 41 59 79 101 125 151 179 209 241 275 311 349 -40 -38 -34 -28 -20 -10 2 16 32 50 70 92 116 142 170 200 232 266 302 340 -53 -51 -47 -41 -33 -23 -11 3 19 37 57 79 103 129 157 187 219 253 289 327 -70 -68 -64 -58 -50 -40 -28 -14 2 20 40 62 86 112 140 170 202 236 272 310 -91 -89 -85 -79 -71 -61 -49 -35 -19 -1 19 41 65 91 119 149 181 215 251 289 -116 -114 -110 -104 -96 -86 -74 -60 -44 -26 -6 16 40 66 94 124 156 190 226 264 -145 -143 -139 -133 -125 -115 -103 -89 -73 -55 -35 -13 11 37 65 95 127 161 197 235 -178 -176 -172 -166 -158 -148 -136 -122 -106 -88 -68 -46 -22 4 32 62 94 128 164 202 -215 -213 -209 -203 -195 -185 -173 -159 -143 -125 -105 -83 -59 -33 -5 25 57 91 127 165
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[fmaxval, fmaxloc] = max(f, [], 'all')
fmaxval = 354
fmaxloc = 248
What does that location of 248 correspond to in terms of x and y?
[row, col] = ind2sub(size(f), fmaxloc)
row = 1
col = 20
fprintf("Maximum of f is %d at x = %d, y = %d.\n", fmaxval, col, row)
Maximum of f is 354 at x = 20, y = 1.
Note that the index into x is the col index and the index into y is the row index. Let's check by evaluating the function once more at those specific values of x and y.
fh(x(col), y(row))
ans = 354
That matches the maximum value found by max (and by inspection of the displayed f array.)
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

VBBV 2024년 5월 29일 3:18
x = 1:20;
y = 1:13;
f = [];
for i = 1:length(x),
for j = 1:length(y);
f(i,j) = x(i).^2 - x(i) -2*y(j).^2 + y(j) -25;
end
end
f
f = 20x13
-26 -31 -40 -53 -70 -91 -116 -145 -178 -215 -256 -301 -350 -24 -29 -38 -51 -68 -89 -114 -143 -176 -213 -254 -299 -348 -20 -25 -34 -47 -64 -85 -110 -139 -172 -209 -250 -295 -344 -14 -19 -28 -41 -58 -79 -104 -133 -166 -203 -244 -289 -338 -6 -11 -20 -33 -50 -71 -96 -125 -158 -195 -236 -281 -330 4 -1 -10 -23 -40 -61 -86 -115 -148 -185 -226 -271 -320 16 11 2 -11 -28 -49 -74 -103 -136 -173 -214 -259 -308 30 25 16 3 -14 -35 -60 -89 -122 -159 -200 -245 -294 46 41 32 19 2 -19 -44 -73 -106 -143 -184 -229 -278 64 59 50 37 20 -1 -26 -55 -88 -125 -166 -211 -260
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fmax = max(max(f));
display(fmax)
fmax = 354
##### 댓글 수: 6이전 댓글 4개 표시이전 댓글 4개 숨기기
Akira Agata 2024년 5월 29일 3:47
+1
By using meshgrid, you can avoid for-loop, like:
x = 1:20;
y = 1:13;
% Create x- and y-grid
[xGrid, yGrid] = meshgrid(x, y);
% Calculate f
f = xGrid.^2 - xGrid - 2*(yGrid.^2) + yGrid -25;
% Find max(f) and it's linear index
[fmax, idx] = max(f(:));
% Show the result
sprintf('max(f) = %d', fmax)
ans = 'max(f) = 354'
sprintf('(x,y) = (%d, %d)', xGrid(idx), yGrid(idx))
ans = '(x,y) = (20, 1)'
% Visualize f
figure
surf(xGrid, yGrid, f)
VBBV 2024년 5월 29일 11:37
@akira Agata what does "+1' mean ?

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by