Clear Filters
Clear Filters

I am writing a code for using "fmincon" but it is having some error, Why?

2 views (last 30 days)
I am writing the following code. I am not able to find the error? I am new to MATLAB.
dbstop if error
clear all
clc
format longEng
kv = 0.5; kh = 0.1;
r0 = 5; H = 5;
q = 20; a = 1;
b = 0; gamma = 18.4;
omega = 25; phi = 39*(pi/180);
lambda = kv/kh;
syms x(i)
% fun = @(x)A1 /((lambda*A1)+A2)
L = r0*(sin(x(2))*exp((x(2)-x(1))*tan(phi))-sin(x(1)))
% % % % Rate of work due to soil weight in static condition
f1 = -(1/3)*(1/(9*((tan(phi))^2)+1)*(exp(3*(x(2) - x(1))*tan(phi))*(3*tan(phi)*sin(x(2))-cos(x(2)))-(3*tan(phi)*sin(x(1))-cos(x(1)))))
f2 = -(1/6)*(L/r0)*cos(x(2))*exp(x(2)-x(1))*tan(phi)*(2*sin(x(1))+(L/r0))
f3 = -(1/3)*(H/r0)*(sin(x(1)))^2
W1 = (1-kv)*gamma*omega*((r0)^3)*(f1-f2-f3)
% Rate of work due to strip load
Wqs = -(1-kv)*omega*q*b*(a+(b/2)+r0*sin(x(1)))
% % % % Rate of work due to soil weight with kh
fq1 = -(1/3)*(1/(9*((tan(phi))^2)+1)*(exp(3*(x(2) - x(1))*tan(phi))*(3*tan(phi)*cos(x(2))+sin(x(2)))-(3*tan(phi)*cos(x(1))+sin(x(1)))))
fq2 = -(1/3)*(L/(r0)^2)*cos(x(2))*exp(x(2)-x(1))*tan(phi)*(cos(x(1))-(H/r0))
fq3 = -(1/6)*(H/r0)*sin(x(1))*(2*cos(x(1))-(H/r0))
W1kh = kh*gamma*omega*((r0)^3)*(fq1-fq2-fq3)
% Rate of work due to strip load
Wqskh = -kh*omega*q*b*r0*exp((x(2)-x(1))*tan(phi))*cos(x(2))
% Yield acceleration coefficients (ky)
A1 = gamma*omega*((r0)^3)*(f1-f2-f3) - omega*q*b*(a+(b/2)+r0*sin(x(1)))
A2 = omega*q*b*r0*exp((x(2)-x(1))*tan(phi))*cos(x(2)) - gamma*omega*((r0)^3)*(fq1-fq2-fq3)
fun = @(x)A1 /((lambda*A1)+A2)
lb = [0,0];
ub = [3.14,3.14];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0,0];
nonlcon = [];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

Accepted Answer

Matt J
Matt J on 13 May 2022
Your objective function is not returning numbers. It is returning sym variables, e.g.,
>> fun([0,0])
ans =
(6.7465e+03*sin(x(1)) - 2.7771e+03*cos(x(1)) + 2.7771e+03*exp(2.4294*x(2) - 2.4294*x(1))*(cos(x(2)) - 2.4294*sin(x(2))) + 1.9167e+04*sin(x(1))^2 - 4.6563e+04*exp(x(2) - 1*x(1))*cos(x(2))*(sin(x(1)) + exp(0.8098*x(2) - 0.8098*x(1))*sin(x(2)))*(0.1667*sin(x(1)) - 0.1667*exp(0.8098*x(2) - 0.8098*x(1))*sin(x(2))))/(3.0955e+04*sin(x(1)) - 2.0632e+04*cos(x(1)) + 1.3885e+04*exp(2.4294*x(2) - 2.4294*x(1))*(cos(x(2)) - 2.4294*sin(x(2))) + 2.7771e+03*exp(2.4294*x(2) - 2.4294*x(1))*(2.4294*cos(x(2)) + sin(x(2))) + 9.5833e+04*sin(x(1))^2 - 9.5833e+03*sin(x(1))*(2*cos(x(1)) - 1) - 2.3281e+05*exp(x(2) - 1*x(1))*cos(x(2))*(sin(x(1)) + exp(0.8098*x(2) - 0.8098*x(1))*sin(x(2)))*(0.1667*sin(x(1)) - 0.1667*exp(0.8098*x(2) - 0.8098*x(1))*sin(x(2))) + 4.6563e+04*exp(x(2) - 1*x(1))*cos(x(2))*(cos(x(1)) - 1)*(0.0667*sin(x(1)) - 0.0667*exp(0.8098*x(2) - 0.8098*x(1))*sin(x(2))))
>> whos ans
Name Size Bytes Class Attributes
ans 1x1 8 sym
  2 Comments
Matt J
Matt J on 13 May 2022
lb = [0,0];
ub = [3.14,3.14];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0,0];
nonlcon = [];
x = fmincon(@fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
2.2538 3.1342
function fval = fun(x)
kv = 0.5; kh = 0.1;
r0 = 5; H = 5;
q = 20; a = 1;
b = 0; gamma = 18.4;
omega = 25; phi = 39*(pi/180);
lambda = kv/kh;
% fun = @(x)
L = r0*(sin(x(2))*exp((x(2)-x(1))*tan(phi))-sin(x(1)));
% % % % Rate of work due to soil weight in static condition
f1 = -(1/3)*(1/(9*((tan(phi))^2)+1)*(exp(3*(x(2) - x(1))*tan(phi))*(3*tan(phi)*sin(x(2))-cos(x(2)))-(3*tan(phi)*sin(x(1))-cos(x(1)))));
f2 = -(1/6)*(L/r0)*cos(x(2))*exp(x(2)-x(1))*tan(phi)*(2*sin(x(1))+(L/r0));
f3 = -(1/3)*(H/r0)*(sin(x(1)))^2;
W1 = (1-kv)*gamma*omega*((r0)^3)*(f1-f2-f3);
% Rate of work due to strip load
Wqs = -(1-kv)*omega*q*b*(a+(b/2)+r0*sin(x(1)));
% % % % Rate of work due to soil weight with kh
fq1 = -(1/3)*(1/(9*((tan(phi))^2)+1)*(exp(3*(x(2) - x(1))*tan(phi))*(3*tan(phi)*cos(x(2))+sin(x(2)))-(3*tan(phi)*cos(x(1))+sin(x(1)))));
fq2 = -(1/3)*(L/(r0)^2)*cos(x(2))*exp(x(2)-x(1))*tan(phi)*(cos(x(1))-(H/r0));
fq3 = -(1/6)*(H/r0)*sin(x(1))*(2*cos(x(1))-(H/r0));
W1kh = kh*gamma*omega*((r0)^3)*(fq1-fq2-fq3);
% Rate of work due to strip load
Wqskh = -kh*omega*q*b*r0*exp((x(2)-x(1))*tan(phi))*cos(x(2));
% Yield acceleration coefficients (ky)
A1 = gamma*omega*((r0)^3)*(f1-f2-f3) - omega*q*b*(a+(b/2)+r0*sin(x(1)));
A2 = omega*q*b*r0*exp((x(2)-x(1))*tan(phi))*cos(x(2)) - gamma*omega*((r0)^3)*(fq1-fq2-fq3);
fval = A1 /((lambda*A1)+A2);
end

Sign in to comment.

More Answers (0)

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!