fminbnd not working for storing values in array.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I keep getting this error.
Error in ScratchWork (line 12)
[Vmin(i),DragMin(i)] = fminbnd(Drag,0,15000);
I don't know what wrong.
this is the code
s = 0.6;
W = linspace(12000,20000,100);
%array of W values given
Drag = @(V) (0.01*s*V^2) + (0.95/s).*((W./V)^2);
Vmin = zeros(1,length(W));
DragMin = zeros(1,length(W));
for i = 1:1:length(W) %incrementing for loop by 1 to the length of array W
[Vmin(i),DragMin(i)] = fminbnd(Drag,0,15000);
%calling fminbnd function for each new W value to store in each V and D with respect to ii
end
hold on
grid on
plot(W, Vmin, 'r', W, DragMin, 'g');
legend('Minimum Velocity', 'Minimum Drag');
xlabel('Weight(W)');
title('Sensitivity Analysis');
0 comentarios
Respuestas (2)
Walter Roberson
el 31 de En. de 2019
Your W is a vector, so in
Drag = @(V) (0.01*s*V^2) + (0.95/s).*((W./V)^2);
then you are calculating a vector, but fmincon needs to a scalar.
for i = 1:1:length(W) %incrementing for loop by 1 to the length of array W
Drag = @(V) (0.01*s*V^2) + (0.95/s).*((W(i)./V)^2);
[Vmin(i),DragMin(i)] = fminbnd(Drag,0,15000);
%calling fminbnd function for each new W value to store in each V and D with respect to ii
end
Star Strider
el 31 de En. de 2019
There are two errors.
The first is with respect to your needing to vectorise your ‘Drag’ function, and this will work:
Drag = @(V) (0.01*s*V.^2) + (0.95/s).*((W./V).^2);
and the second is that ‘Drag’ must return a scalar value, and this will work for that:
[Vmin(i),DragMin(i)] = fminbnd(@(V)norm(Drag(V)),0,15000);
Although I strongly suspect that what you intend is to define ‘Drag’ as:
Drag = @(V,W) (0.01*s*V.^2) + (0.95/s).*((W./V).^2);
and then optimise it in the loop as:
[Vmin(i),DragMin(i)] = fminbnd(@(V)Drag(V,W(i)),0,15000);
This does not require that you re-define ‘Drag’ in every iteration of your loop. Just call it with the new value of ‘W(i)’ instead. This is likely much more efficient.
1 comentario
Walter Roberson
el 31 de En. de 2019
Editada: Walter Roberson
el 31 de En. de 2019
You do not need to vectorize Drag. For fminbnd
fun is a function that accepts a real scalar x and returns a real scalar f
Redefining Drag each iteration is more efficient, as then fminbnd is only calling through one anonymous function instead of two (Drag and the wrapping anonymous function.)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!