MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Shantanu K on 7 Mar 2013

In this i have to do following operation mentioned:

Here value of a,b,z are changing continuously. For every value program will calculate value of 'y'.

It will become three dimensional array.

For entire these iterations i have to find out maximum value of 'y' and corresponding value of 'a' & 'b'.

I am facing one more problem, when i formulated array it was showing lot more zeros.

for p=1:6 a=2*p; for q=1:6 b=5*q;

for z=1:100 k=z*z; x=k*4+1; y=a*x+b*k+1; y(p,q,z)=y;

end end end

*No products are associated with this question.*

Answer by Andrei Bobrov on 7 Mar 2013

Edited by Andrei Bobrov on 7 Mar 2013

Accepted answer

max1 = [-inf 0 0 0]; for p=1:6 for q=1:6 for z=1:100 a=2*p; b=5*q; k=z*z; x=k*4+1; k1 = a*x+b*k+1; y(p,q,z) = k1; if k1 > max1(1), max1 = [k1,p,q,z]; end end end end

Answer by Walter Roberson on 7 Mar 2013

Before loop:

maxy = -inf; a_at_max = -inf; b_at_max = -inf;

then inside the loop, where you assign into y, test to see if the new value is greater than maxy, and if it is, record the a and b values

Shantanu K on 7 Mar 2013

I am not able to understand it sir, can you please elaborate?

**then inside the loop, where you assign into y, test to see if the new value is greater than maxy, and if it is, record the a and b values**
This part i am not able to convert to program.

Walter Roberson on 7 Mar 2013

if y(p,q,z) > maxy %then update your record of which "a" and "b" values led to this maximum end

Oh yes and remember to update maxy with the new maximum as well.

Hint: "update" means "do an assignment to"

## 6 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134721

Your statement y=a*x+b*k+1; is completely overwriting any previous value of the variable "y", including the use of "y" as an array from the previous iteration of the very next line.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134753

Thanks Walter, can you guide me in finding the maximum value of 'y' & corresponding 'a' & 'b' values

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134757

"y" cannot be simultaneously a scalar and an array. You need to use a different name for one of the two. Or just skip the temporary variable and use

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134793

How to find maximum value of 'y' and get corresponding value of 'p' and 'q'?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134795

Do you want the 'a' and 'b' (as you wrote the first time) or the 'p' and 'q' (as you write now) ?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/66209#comment_134866

Thanks Walter for your help !