Hello,
I'm new to matlab and need help making a code for obtaining an optimize value. Lets say I have y=a^2-8*b+C*2/ab for example and I have to obtain y<x where a=1:5, b=30:100 and c=20:30.
So I first use an initial value for a, b, and c, then if y<x is not met it will recalculate with a different value a,b,c until y<x is satisfied. Basically making a loop.
Is it possible to code this?
Thanks in advance

 Respuesta aceptada

Ahmed raafat
Ahmed raafat el 29 de Jul. de 2017
why loop??? use this beautiful method
a=1:5;
b=30:100;
c=20:30;
[a,b,c]=meshgrid(a,b,c);
y=a.^2+8*b+2.*c./a./b;
This will produce matrix with size(71,5,11)
you can get the minimum result in the next line
x=min(min(min(y)));

4 comentarios

Ahmed raafat
Ahmed raafat el 29 de Jul. de 2017
Editada: Ahmed raafat el 29 de Jul. de 2017
if you want all values of y<x
[r1,r2,r3]=find(y<x);
Close, but to get the proper indices you should use
idx = find(y<x);
[r1, r2, r3] = ind2sub(size(y), idx);
The reason for this is that when you use three outputs to find(), the third output is the value at the location, which is especially useful for sparse arrays.
Zebedee Mojuntin
Zebedee Mojuntin el 30 de Jul. de 2017
Cheers guys!
JohnB
JohnB el 31 de En. de 2020
Hi, and where can you get the values of a_min, b_min and c_min to obtain the minimum ?
Regards

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Jul. de 2017

Comentada:

el 31 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by