Balraj Boyal
Balraj Boyal

Reputation: 155

MATLAB: Using FZERO on a function which has a vector output

I am working on my thesis and running in some programming problems in Matlab. I am trying to implement the ''golden Bisection Method'' to speed up my code. To this end, I've consulted the build in function FZERO.

So I am determining the difference between two vectors which are both (1x20).

Difference = Clmax_dist-cl_vec;  

Clmax_dist comes from a semi-empirical method and cl_vec comes from the excecution of an external AVL.exe file.

Essentially, this difference depends only on one single variable AOA because the Clmax_dist vector is a constant. Hence, I am constantly feeding a new AOA value to the AVL.exe to obtain a new cl_vec and compare this again to the constant Clmax_dist.

I am iterating this until one of the element in the vector becomes either zero or negative. My loop stops and reveals the final AOA. This is a time consuming method and I wanted to use FZERO to speed this up.

However, the FZERO documentation reveals that it only works on function which has a scalar as input. Hence, my question is: How can I use FZERO with a function which has a vector as an output. Or do i need to do something totally different?

I've tried the following:

[Difference] = obj.DATCOMSPANLOADING(AOA);

[email protected];
AOA_init = [1 20];
AOA_root = fzero(fun,AOA_init,'iter');

this gave me the following error:

Operands to the || and && operators must be convertible to logical scalar values.

Error in fzero (line 423)
while fb ~= 0 && a ~= b

Error in CleanCLmax/run (line 11)
AOA_root = fzero(fun,AOA_init,'iter');

Error in InitiatorController/moduleRunner (line 11)
        ModuleHandle.run;

Error in InitiatorController/runModule (line 95)
            obj.moduleRunner(ModuleHandle);

Error in RunSteps (line 7)
C.runModule('CleanCLmax');

The DATCOMSPANDLOADING function contains the following:

function [Difference] = DATCOMSPANLOADING(obj,AOA)

[Input]= obj.CLmaxInput;                                       % Creates Input structure and airfoil list
obj.writeAirfoils(Input);                                                  % Creates airfoil coordinate files in AVL directory
[Clmax_dist,YClmax,Cla_mainsections] = obj.Clmax_spanwise(Input);          % Creates spanwise section CLmax with ESDU method
[CLa] = obj.WingLiftCurveSlope(Input,Cla_mainsections);                    % Wing lift curve slope






[Yle_wing,cl_vec] = obj.AVLspanloading(Input,CLa,AOA);                      % Creates spanloading with AVL




         Difference = Clmax_dist-cl_vec;  

end

If I need to elaborate further, feel free to ask. And of course, Thank you very much.

Upvotes: 1

Views: 1280

Answers (1)

Jonas
Jonas

Reputation: 74940

fzero indeed only works on scalars. However, you can turn your criterion into a scalar: You are interested in AOA where any of the elements in the vector becomes zero, in which case you rewrite your objective function to return two output arguments: minDifference, which is min(Difference), and Difference. The first output, minDifference is the minimum of the difference, i.e. what fzero should try to optimize (from your question, I'm assuming all values start positive). The second output you'd use to inspect your difference vector in the end.

Upvotes: 3

Related Questions