To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
Dina
Getting settled in
Dina
5 Posts

matlab solver - grid search

Hi,

I have a problem with matlab.
I need a solver that can change three variables as follows:
x1 = x1+0.001
x2 = x2+0.001
x3 = x3+0.001
until another parameter y < 0.1

I now made it with 3 for loops, but this is way to slow for the interval i have to variate the variables.

Does anyone know a quicker way, like a solver, because i dont understand the matlab help concerning solvers.

Thanks in advance.
shwaip
elaborate bot
shwaip
5,729 Posts

Re: matlab solver

can you post your code please, it'll make it easier for me to understand what you want to do.

please copy and paste it in between
PHP Code:
[code]CODE HERE![/code
tags!
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
Dina
Getting settled in
Dina
5 Posts

Re: matlab solver

Code:
 
%%Constants%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rho0 = 1.225;
labda = -0.0065;
T0 = 288.15;
g0 = 9.80665;
R = 287.05;
 
%%Aircraft 1 parameters (index 1)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S_1 = 154;
W_1 = [0 0 -17463.49];
Cd0_1 = 0.01232;
AR_1 = 23;
e_1 = 0.8765;
 
%%Aircraft 2 parameters (index 2)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S_2 = 154;
W_2 = [0 0 -17463.49];
Cd0_2 = 0.01232;
AR_2 = 23;
e_2 = 0.8765;
 
 
%%Cable parameters%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sigma_ultimate = 1.1E9;
j = 2;
rho_c = 700;
 
%%Inputs%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Jet stream inputs
H_delta = 1978.50;
W_delta = 16.48;
windratio = 0.7;
 
%Aircraft 1 inputs
AOA_1 = 9;
Vy_1 = [0 29 0];
Vz_1 = [0 0 0];
H_cruise_1 = 11000;
 
%Kite inputs
Vz_2 = [0 0 0];
 
%Cable inputs
d_c = 5;
 
%%Atmosphere calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rho_1 = rho0*(1+labda*H_cruise_1/T0)^(-(g0/(R*labda)+1));
 
H_cruise_2 = H_cruise_1 - H_delta;
rho_2 = rho0*(1+labda*H_cruise_2/T0)^(-(g0/(R*labda)+1));
 
%%Aircraft calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Lift and drag coefficient
Cl_1 = AOA_1*pi/180*2*pi*AR_1/(2+sqrt(4+AR_1^2));
Cd_1 = Cd0_1 + Cl_1^2/(pi*AR_1*e_1);
 
%Speed
Vx_1 = [W_delta*windratio 0 0];
V_1 = Vx_1 + Vy_1 + Vz_1;
V_1_size = sqrt(sum(V_1.^2));
 
%%LOOP: Change phi_1 until Fy_1 = 0%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
phi_1 = 0;
T(2) = 1;
 
while(abs(T(2)) > 0.01) 
phi_1 = phi_1+0.0001;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%Lift and drag
L_1_size = 0.5*rho_1*V_1_size^2*Cl_1*S_1;
L_1 = [-L_1_size*sin(phi_1*pi/180)*Vy_1(2)/V_1_size L_1_size*sin(phi_1*pi/180)*Vx_1(1)/V_1_size L_1_size*cos(phi_1*pi/180)];
 
D_1_size = 0.5*rho_1*V_1_size^2*Cd_1*S_1;
D_1 = [-D_1_size*Vx_1(1)/V_1_size -D_1_size*Vy_1(2)/V_1_size 0];
 
%%Cable calculations 1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Cable height 1
H_cable_1 = H_delta*windratio;
 
%Maximum cable tension
T_max = sigma_ultimate/j*(d_c/1000)^2*pi/4;
 
%Cable drag 1
Dx_c1 = -1/3*1/2*rho_1*Vx_1.^2*d_c/1000*H_cable_1;
Dy_c1 = -1/3*1/2*rho_1*Vy_1.^2*d_c/1000*H_cable_1;
Dz_c1 = -1/3*1/2*rho_1*Vz_1.^2*d_c/1000*H_cable_1;
D_c1 = Dx_c1 + Dy_c1 + Dz_c1;
 
%%Tension force equilibrium%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fx_1 = [L_1(1)+D_1(1)+D_c1(1) 0 0];
Fy_1 = [0 L_1(2)+D_1(2)+D_c1(2) 0];
Fz_1 = [0 0 L_1(3)+ D_1(3)+ D_c1(3)+W_1(3)];
 
T = Fx_1 + Fy_1 + Fz_1;
T_size = sqrt(sum(T.^2));
 
%cable angle
beta_1 = atan(Fz_1(3)/Fx_1(1))*180/pi;
 
end
 
%%Kite calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%LOOP: vary AOA_2, Vy_2, phi_2 so that T1 = T2%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Vy_2 = [0 0 0];
 
for AOA_2 = -2:0.1:14
for i = -40:0.1:40
Vy_2(2) = i;
for phi_2 = -90:0.1:90
%Lift and drag coefficient
Cl_2 = AOA_2*pi/180*2*pi*AR_2/(2+sqrt(4+AR_2^2));
Cd_2 = Cd0_2 + Cl_2^2/(pi*AR_2*e_2);
 
%Speed
Vx_2 = [W_delta-Vx_1(1) 0 0];
V_2 = Vx_2 + Vy_2 + Vz_2;
V_2_size = sqrt(sum(V_2.^2));
 
%Lift and drag
L_2_size = 0.5*rho_2*V_2_size^2*Cl_2*S_2;
L_2 = [-L_2_size*sin(phi_2*pi/180)*Vy_2(2)/V_2_size L_2_size*sin(phi_2*pi/180)*Vx_2(1)/V_2_size L_2_size*cos(phi_2*pi/180)];
 
D_2_size = 0.5*rho_2*V_2_size^2*Cd_2*S_2*sign(V_2_size);
D_2 = [-D_2_size*Vx_2(1)/V_2_size -D_2_size*Vy_2(2)/V_2_size 0];
 
%%Cable calculations 1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Cable height 1
H_cable_2 = H_delta - H_cable_1;
 
%Cable drag 1
Dx_c2 = -1/3*1/2*rho_2*Vx_2.^2*d_c/1000*H_cable_2;
Dy_c2 = -1/3*1/2*rho_2*Vy_2.^2*d_c/1000*H_cable_2;
Dz_c2 = -1/3*1/2*rho_2*Vz_2.^2*d_c/1000*H_cable_2;
D_c2 = Dx_c2 + Dy_c2 + Dz_c2;
 
%%Tension force equilibrium%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fx_2 = [L_2(1)+D_2(1)+D_c2(1) 0 0];
Fy_2 = [0 L_2(2)+D_2(2)+D_c2(2) 0];
Fz_2 = [0 0 L_2(3)+ D_2(3)+ D_c2(3)+W_2(3)];
 
T_2 = Fx_2 + Fy_2 + Fz_2;
T_size_2 = sqrt(sum(T.^2));
 
%Cable angle
beta_2 = atan(Fz_2(3)/Fx_2(1))*180/pi;
 
dFx = Fx_1(1) - Fx_2(1);
dFy = Fy_1(2)^2 + Fy_2(2)^2;
dFz = Fz_1(3) + Fz_2(3);
dF = dFx^2 + dFy^2 +dFz^2;
 
if dF < 1
break;
end
end
 
if dF < 1
break;
end
end
 
if dF < 1
break;
end
end
shwaip
elaborate bot
shwaip
5,729 Posts

Re: matlab solver

Soo...do you know if the optimization problem is convex? Because there are much better ways to do convex optimization than a linear search. I don't think there's a really quick way to do the linear search in matlab...you might be able to do it a bit faster with matricies instead of for loops, but i dunno how much that'll help.

one easy way to do this is to change the step size on your loops (make them like .5 instead of .1). If you find a value that works, then great. If you don't, then take the space that corresponds to the smallest value, and do the search with the smaller step size.
Dina
Getting settled in
Dina
5 Posts

Re: matlab solver

I don't really know if it's convex, because I never heard about convex optimization. Is such a optimization easy to do?

Increasing the stepsize is not really the option, because it will not find a solution then.
shwaip
elaborate bot
shwaip
5,729 Posts

Re: matlab solver

Does the code that you gave me have a solution? I want to see if there's an error in my code.

On thing to think about is if you really want to search for the answer over the entire space - phi is probably some angle (correct) - would you expect to get an answer that had |phi| > 80?

This code does what is called a grid search. It steps over the space at a large step size (.5), finds the best value at that step size. Then, it looks at a range near that optimum at a smaller step size(.1), and finds the best value at that step size. This is repeated at (.01).

There is an error in the printf statement (at least on my version of matlab), but it is working.

PHP Code:
%%Constants%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rho0 1.225;
labda = -0.0065;
T0 288.15;
g0 9.80665;
287.05;
 
%%
Aircraft 1 parameters (index 1)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S_1 154;
W_1 = [0 0 -17463.49];
Cd0_1 0.01232;
AR_1 23;
e_1 0.8765;
 
%%
Aircraft 2 parameters (index 2)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S_2 154;
W_2 = [0 0 -17463.49];
Cd0_2 0.01232;
AR_2 23;
e_2 0.8765;
 
 
%%
Cable parameters%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sigma_ultimate 1.1E9;
2;
rho_c 700;
 
%%
Inputs%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
Jet stream inputs
H_delta 
1978.50;
W_delta 16.48;
windratio 0.7;
 
%
Aircraft 1 inputs
AOA_1 
9;
Vy_1 = [0 29 0];
Vz_1 = [0 0 0];
H_cruise_1 11000;
 
%
Kite inputs
Vz_2 
= [0 0 0];
 
%
Cable inputs
d_c 
5;
 
%%
Atmosphere calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rho_1 rho0*(1+labda*H_cruise_1/T0)^(-(g0/(R*labda)+1));
 
H_cruise_2 H_cruise_1 H_delta;
rho_2 rho0*(1+labda*H_cruise_2/T0)^(-(g0/(R*labda)+1));
 
%%
Aircraft calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
Lift and drag coefficient
Cl_1 
AOA_1*pi/180*2*pi*AR_1/(2+sqrt(4+AR_1^2));
Cd_1 Cd0_1 Cl_1^2/(pi*AR_1*e_1);
 
%
Speed
Vx_1 
= [W_delta*windratio 0 0];
V_1 Vx_1 Vy_1 Vz_1;
V_1_size sqrt(sum(V_1.^2));
 
%%
LOOPChange phi_1 until Fy_1 0%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
phi_1 0;
T(2) = 1;
while(
abs(T(2)) > 0.01
    
phi_1 phi_1+0.0001;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %
Lift and drag
    L_1_size 
0.5*rho_1*V_1_size^2*Cl_1*S_1;
    
L_1 = [-L_1_size*sin(phi_1*pi/180)*Vy_1(2)/V_1_size L_1_size*sin(phi_1*pi/180)*Vx_1(1)/V_1_size L_1_size*cos(phi_1*pi/180)];

    
D_1_size 0.5*rho_1*V_1_size^2*Cd_1*S_1;
    
D_1 = [-D_1_size*Vx_1(1)/V_1_size -D_1_size*Vy_1(2)/V_1_size 0];

    %%
Cable calculations 1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
Cable height 1
    H_cable_1 
H_delta*windratio;

    %
Maximum cable tension
    T_max 
sigma_ultimate/j*(d_c/1000)^2*pi/4;

    %
Cable drag 1
    Dx_c1 
= -1/3*1/2*rho_1*Vx_1.^2*d_c/1000*H_cable_1;
    
Dy_c1 = -1/3*1/2*rho_1*Vy_1.^2*d_c/1000*H_cable_1;
    
Dz_c1 = -1/3*1/2*rho_1*Vz_1.^2*d_c/1000*H_cable_1;
    
D_c1 Dx_c1 Dy_c1 Dz_c1;

    %%
Tension force equilibrium%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
Fx_1 = [L_1(1)+D_1(1)+D_c1(10 0];
    
Fy_1 = [0 L_1(2)+D_1(2)+D_c1(20];
    
Fz_1 = [0 0 L_1(3)+ D_1(3)+ D_c1(3)+W_1(3)];

    
Fx_1 Fy_1 Fz_1;
    
T_size sqrt(sum(T.^2));

    %
cable angle
    beta_1 
atan(Fz_1(3)/Fx_1(1))*180/pi;
end
%kite calculations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%
LOOPvary AOA_2Vy_2phi_2 so that T1 T2%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Vy_2 = [0 0 0];

minA=0;
minI=0;
minPhi=0;
minVal=inf;

ARange =[-2 14];
iRange =[-40 40];
phiRange = [-90 90]

for 
step = [.5 .1 .01]
    for 
AOA_2 ARange(1):step:ARange(2)
        for 
iRange(1):step:iRange(2)
            
Vy_2(2) = i;
            for 
phi_2 phiRange(1):step:phiRange(2)
                %
Lift and drag coefficient
                Cl_2 
AOA_2*pi/180*2*pi*AR_2/(2+sqrt(4+AR_2^2));
                
Cd_2 Cd0_2 Cl_2^2/(pi*AR_2*e_2);

                %
Speed
                Vx_2 
= [W_delta-Vx_1(10 0];
                
V_2 Vx_2 Vy_2 Vz_2;
                
V_2_size sqrt(sum(V_2.^2));

                %
Lift and drag
                L_2_size 
0.5*rho_2*V_2_size^2*Cl_2*S_2;
                
L_2 = [-L_2_size*sin(phi_2*pi/180)*Vy_2(2)/V_2_size L_2_size*sin(phi_2*pi/180)*Vx_2(1)/V_2_size L_2_size*cos(phi_2*pi/180)];

                
D_2_size 0.5*rho_2*V_2_size^2*Cd_2*S_2*sign(V_2_size);
                
D_2 = [-D_2_size*Vx_2(1)/V_2_size -D_2_size*Vy_2(2)/V_2_size 0];

                %%
Cable calculations 1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %
Cable height 1
                H_cable_2 
H_delta H_cable_1;

                %
Cable drag 1
                Dx_c2 
= -1/3*1/2*rho_2*Vx_2.^2*d_c/1000*H_cable_2;
                
Dy_c2 = -1/3*1/2*rho_2*Vy_2.^2*d_c/1000*H_cable_2;
                
Dz_c2 = -1/3*1/2*rho_2*Vz_2.^2*d_c/1000*H_cable_2;
                
D_c2 Dx_c2 Dy_c2 Dz_c2;

                %%
Tension force equilibrium%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                
Fx_2 = [L_2(1)+D_2(1)+D_c2(10 0];
                
Fy_2 = [0 L_2(2)+D_2(2)+D_c2(20];
                
Fz_2 = [0 0 L_2(3)+ D_2(3)+ D_c2(3)+W_2(3)];

                
T_2 Fx_2 Fy_2 Fz_2;
                
T_size_2 sqrt(sum(T.^2));

                %
Cable angle
                beta_2 
atan(Fz_2(3)/Fx_2(1))*180/pi;

                
dFx Fx_1(1) - Fx_2(1);
                
dFy Fy_1(2)^Fy_2(2)^2;
                
dFz Fz_1(3) + Fz_2(3);
                
dF dFx^dFy^+dFz^2;

                if 
dF 1
                    printf
('done');
                    break;
                elseif 
dF minVal
                    minA
=AOA_2;
                    
minI=i;
                    
minPhi=phi_2;
                    
minVal dF
                    
                end
            end

%             if dF 1
%                 printf('done');
%                 break;
%             
end
        end

%         if dF 1
%             printf('done');
%             break;
%         
end
    end
    ARange 
= [minA-4*step minA+4*step]
    
iRange = [minI-4*step minI+4*step]
    
phiRange = [minPhi-4*step minPhi+4*step]
end 
Dina
Getting settled in
Dina
5 Posts

Re: matlab solver

hey thanks very much for your time. It is kind of working. The solution is there if the components of T are equal to the components of T_2. This is not yet the case within a certain accuracy. Decreasing the stepsize helps a bit, but not enough, and it increases calculating time a lot.
Dina
Getting settled in
Dina
5 Posts

Re: matlab solver

Ii just checked, i need at least an accuracy in stepsize of 0.00001 to get to the right solution. Do you know a way in which to get to a solution faster?
shwaip
elaborate bot
shwaip
5,729 Posts

Re: matlab solver

you can add step sizes to the
for step=[.5 .1 .01]
line to get better accuracy. I wouldn't decrease step size by more than a power of 10 each time, though.

If you're not finding an optimum, you can also increase the "4" below to something a bit larger

ARange = [minA-4*step minA+4*step]
iRange = [minI-4*step minI+4*step
]
phiRange = [minPhi-4*step minPhi+4*step]
Similar Threads
Thread Thread Starter Forum Replies Last Post
Dr Watson Postmortem debugger crash rhygin Resolved / Inactive 26 3 Nov 2006 6:10pm
xsecretacex - Home Search Assisstant, i would apreciate some ones help. xsecretacex Resolved / Inactive 4 15 Jul 2005 1:49pm
Need help getting rid of Home Search Assistent, Search Extender, and Shopping Wizard licia9187 Resolved / Inactive 24 5 Jun 2005 4:09am
can someone help me with get rid of this search extender thing Supafly309 Resolved / Inactive 17 1 Jun 2005 11:25pm
Search Extender, Search Assistant, Shopping Wizard Issue Etin Resolved / Inactive 0 19 Jan 2005 1:39am

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 8:27am (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.