matlab fmincon优化,求教Matlab用fmincon做优化计算

matlab fmincon优化,求教Matlab用fmincon做优化计算本人利用fmincon做优化计算,其程序如下:1,主程序clearallx0=[0.1,0.3,0.2,0.3,0.1,45,0.214,0.05,0,0.45,0.15,0,0.4,0.12,0,0,0,0,0,0,0,0,0,0,0,0];A=[1-1-110000000000000000000000;11-1-10000…

大家好,又见面了,我是你们的朋友全栈君。

本人利用fmincon做优化计算,其程序如下:

1,主程序

clear all

x0=[0.1,0.3,0.2,0.3,0.1,45,0.214,0.05,0,0.45,0.15,0,0.4,0.12,0,0,0,0,0,0,0,0,0,0,0,0];

A=[ 1 -1 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 1 1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 1 -1 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; -2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 -2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 -2 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];

b=[0;0;0;0;0;0];

Aeq=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 4 5 6 7 8 9 10; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 12 20 30 42 56 72 90];

beq=[0;0;0;0];

[x,fval,exitflag]=fmincon(@myfun,x0,A,b,Aeq,beq);

2,优化程序

function c=myfun(x)

% Parameters of the four-bar linkage(m,rad)

l1=x(1);l2=x(2);l3=x(3);l4=x(4);L=x(5); beta=x(6)/180*pi;

m1x=x(7);r1x=x(8);phi1x=x(9);m2x=x(10);r2x=x(11);phi2x=x(12); m3x=x(13);r3x=x(14);phi3x=x(15);

% orignal linkage

m1o=0.20;r1o=0.05;phi1o=0; m2o=0.5;r2o=0.15; phi2o=0;m3o=0.4;r3o=0.12; phi3o=0;I1o=0.00017;I2o=0.00375;I3o=0.00192;

m1=m1o+m1x;  m2=m2o+m2x;  m3=m3o+m3x;

r1=sqrt((m1o*r1o)^2+(m1x*r1x)^2-2*m1o*r1o*m1x*r1x*cos(pi-(phi1x-phi1o)))/m1;

r2=sqrt((m2o*r2o)^2+(m2x*r2x)^2-2*m2o*r2o*m2x*r2x*cos(pi-(phi2x-phi2o)))/m2;

r3=sqrt((m3o*r3o)^2+(m3x*r3x)^2-2*m3o*r3o*m3x*r3x*cos(pi-(phi3x-phi3o)))/m3;

phi1=atan((m1o*r1o*sin(phi1o)+m1x*r1x*sin(phi1x))/(m1o*r1o*cos(phi1o)+m1x*r1x*cos(phi1x)));

phi2=atan((m2o*r2o*sin(phi2o)+m2x*r2x*sin(phi2x))/(m2o*r2o*cos(phi2o)+m2x*r2x*cos(phi2x)));

phi3=atan((m3o*r3o*sin(phi3o)+m3x*r3x*sin(phi3x))/(m3o*r3o*cos(phi3o)+m3x*r3x*cos(phi3x)));

v1=r1x^2+r1^2-2*r1x*r1*cos(phi1x-phi1); u1=r1o^2+r1^2-2*r1o*r1*cos(phi1-phi1o);

v2=r2x^2+r2^2-2*r2x*r2*cos(phi2x-phi2); u2=r2o^2+r2^2-2*r2o*r2*cos(phi2-phi2o);

v3=r3x^2+r3^2-2*r3x*r3*cos(phi3x-phi3); u3=r3o^2+r3^2-2*r3o*r3*cos(phi3-phi3o);

I1=I1o+m1x*v1^2+m1x*u1^2;    I2=I2o+m2x*v2^2+m2x*u2^2;    I3=I3o+m3x*v3^2+m3x*u3^2;

%Parameters of the angle path planning

f=[];g=[];h=[];

%varied input speed(rad,rad/s,rad/s^2)

for t=0:0.01:1;

q1=x(16)+x(17)*t+x(18)*t^2+x(19)*t^3+x(20)*t^4+x(21)*t^5+x(22)*t^6+x(23)*t^7+x(24)*t^8+x(25)*t^9+x(26)*t^10;

omega1=x(17)+2*x(18)*t+3*x(19)*t^2+4*x(20)*t^3+5*x(21)*t^4+6*x(22)*t^5+7*x(23)*t^6+8*x(24)*t^7+9*x(25)*t^8+10*x(26)*t^9;

alpha1=2*x(18)+6*x(19)*t+12*x(20)*t^2+20*x(21)*t^3+30*x(22)*t^4+42*x(23)*t^5+56*x(24)*t^6+72*x(25)*t^7+90*x(26)*t^8;

% the angle of the coupler and crank(m,rad)

d=sqrt(l4^2+l1^2-2*l4*l1*cos(q1));

theted=atan((-l1.*sin(q1))/(l4-l1*cos(q1)));

q2=acos((l2^2+d.^2-l3^2)/(2*d*l2))+theted;

A=2*l4*l3-2*l1*l3*cos(q1);B=-2*l1*l3*sin(q1);C=l4^2+l1^2+l3^2-l2^2-2*l4*l1*cos(-q1);

q3=2*atan((-B-sqrt(B^2-C^2+A^2))/(C-A));

% the calculation of velocity

h1=-(l1*csc(q2-q3)*sin(q1-q3))/l2;

h2=-(l1*csc(q2-q3)*sin(q1-q2))/l3;

omega2=h1*omega1;

omega3=h2*omega1;

u=l1*cos(q1)+L*cos(q2+beta);

v=l1*sin(q1)+L*sin(q2+beta);

g=[g;u];

h=[h;v];

v1x=-r1*omega1*sin(q1+phi1);

v1y=r1*omega1*cos(q1+phi1);

v1=sqrt(v1x^2+v1y^2);

v2x=-l1*omega1*sin(q1)-r2*omega2*sin(q2+phi2);

v2y=l1*omega1*cos(q1)+r2*omega2*cos(q2+phi2);

v2=sqrt(v2x^2+v2y^2);

v3x=-r3*omega3*sin(q3+phi3);

v3y=r3*omega3*cos(q3+phi3);

v3=sqrt(v3x^2+v3y^2);

% the calculation of acceleration

h3=-((l1*cos(q1-q3)+l2*h1^2*cos(q2-q3)-l3*h2^2)*csc(q2-q3))/l2;

h4=((-l1*cos(q1-q2)-l2*h1^2+l3*h2^2*cos(q2-q3))*csc(q2-q3))/l3;

alpha2=h3*omega1^2+h1*alpha1;

alpha3=h4*omega1^2+h2*alpha1;

a1x=-r1*alpha1*sin(q1+phi1)-r1*omega1^2*cos(q1+phi1);

a1y=r1*alpha1*cos(q1+phi1)-r1*omega1^2*sin(q1+phi1);

a1=sqrt(a1x^2+a1y^2);

a2x=-l1*alpha1*sin(q1)-l1*omega1^2*cos(q1)-r2*alpha2*sin(q2+phi2)-r2*omega2^2*cos(q2+phi2);

a2y=l1*alpha1*cos(q1)-l1*omega1^2*sin(q1)+r2*alpha2*cos(q2+phi2)-r2*omega2^2*sin(q2+phi2);

a2=sqrt(a2x^2+a2y^2);

a3x=-r3*alpha3*sin(q3+phi3)-r3*omega3^2*cos(q3+phi3);

a3y=r3*alpha3*cos(q3+phi3)-r3*omega3^2*sin(q3+phi3);

a3=sqrt(a3x^2+a3y^2);

% the calculation of each joint’s force

r1x1=l1*cos(q1)-r1*cos(q1+phi1); r1y1=l1*sin(q1)-r1*sin(q1+phi1);

r2x1=l2*cos(q2)-r2*cos(q2+phi2); r2y1=l2*sin(q2)-r2*sin(q2+phi2);

r3x1=l3*cos(q3)-r3*cos(q3+phi3); r3y1=l3*sin(q3)-r3*sin(q3+phi3);

r2x=r2*cos(q2+phi2); r2y=r2*sin(q2+phi2);

Lx=L*cos(beta+q2); Ly=L*sin(beta+q2);

A=[1,0,1,0,0,0,0,0,0;

0,1,0,1,0,0,0,0,0;

-1,0,0,0,1,0,0,0,0;

0,-1,0,0,0,1,0,0,0;

0,0,0,0,-1,0,1,0,0;

0,0,0,0,0,-1,0,1,0;

-r1y1,r1x1,r1*sin(q1+phi1),-r1*cos(q1+phi1),0,0,0,0,1;

-r2*sin(q2+phi2),r2*cos(q2+phi2),0,0,-r2y1,r2x1,0,0,0;

0,0,0,0,r3y1,-r3x1,r3*sin(q3+phi3),-r3*cos(q3+phi3),0];

B=[m1*a1x;

m1*a1y;

m2*a2x;

m2*a2y;

m3*a3x;

m3*a3y;

I1*alpha1;

I2*alpha2;

I3*alpha3];

X=A\B;

f=[f;X’];

end

%把所有的Fsh,MshA,MshD和TIN都加起来

F41x=f(:,3);F41y=f(:,4);F43x=f(:,7);F43y=f(:,8);TIN=f(:,9);

Fsh=sqrt((F41x+F43x).^2+(F41y+F43y).^2);

MshA=-(F43y*l4+TIN);

MshD=(F41y*l4-TIN);

Fsh=sqrt(sum(Fsh.^2)./101);

%TIN=sqrt(sum(TIN.^2)./101);

MshA=sqrt(sum(MshA.^2)./101);

MshD=sqrt(sum(MshD.^2)./101);

%objective function

c1=sqrt(1/6*(g(14)-0.103)^2+(h(14)-0.220)^2+(g(22)-0.072)^2+(h(22)-0.242)^2+(g(34)+0.004)^2+(h(34)-0.231)^2+(g(51)+0.082)^2+(h(51)-0.149)^2+(g(59)+0.091)^2+(h(59)-0.1)^2+(g(93)-0.036)^2+(h(93)-0.090)^2);

c2=1/3*Fsh+1/3*MshA+1/3*MshD;

c=0.5*c1+0.5*c2;

可是最后出现了:

Warning: Large-scale (trust region) method does not currently solve this type of problem,

using medium-scale (line search) instead.

> In fmincon at 317

In main at 12

Optimization terminated: magnitude of directional derivative in search

direction less than 2*options.TolFun and maximum constraint violation

is less than options.TolCon.

No active inequalities.

望高手们予以解答,不甚感激啊!

[本帖最后由 DHU 于 2011-3-17 21:44 编辑]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134004.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ODrive配置云台电机GIMBLE_TYPE

    文章目录0.前言1.硬件介绍1.1.硬件1.2.接线2.配置参数2.1.基本配置2.2.电机配置2.5.保存配置3.校准工作3.1.校准电机3.2.编码器校准3.3.保存校准结果4.切换电机类型5.控制电机运行6.重新启动自动进入闭环控制7.PID参数调节7.1.官网教程7.2.云台电机自己调参总结8.总结8.1.遇到的错误1.MOTOR_ERROR_PHASE_RESISTANCE_OUT_OF_RANGE2.ENCODER_ERROR_CPR_POLEPAIRS_MISMATCH3.CONTROLLE

    2022年4月10日
    60
  • Pytest(10)assert断言[通俗易懂]

    Pytest(10)assert断言[通俗易懂]前言断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试failed

    2022年7月30日
    3
  • 手机看Typora笔记[通俗易懂]

    接触Typora之后感觉还挺好用的,不用上网,不用花钱(白嫖的玩意就是香)。可以写普通的文本,可以添加五个等级的标题,可以插入图片,,还可以插入各种各样的代码块(java,c,html,css等等),简洁而强大。但是当初我以为只能在电脑上看的时候就觉得有点难受,玛德是我S13了,我还去网上搜了半天,看看有没有教程或者手机版的软件,有的还要付费,今天我恍然大悟,尼玛人家开发个软件怎么可能没想到这些东西呢,我真是S13卧槽。看图啥都懂了:别被坑钱和C币吧…

    2022年4月3日
    198
  • phpstorm最新激活码【中文破解版】「建议收藏」

    (phpstorm最新激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    61
  • input file多选 multiple[通俗易懂]

    input file多选 multiple[通俗易懂]一直以为连点2次选择文件是多选,原来要按ctrl选中多个才是多选。。。 functionShowFileName(){ varfile; for(vari=0;document.getElementById(“file”).files.length;i++){ file=document.getElementById(“fil

    2022年7月17日
    10
  • Oracle listagg去重distinct三种方法总结

    Oracle listagg去重distinct三种方法总结一、简介最近在工作中,在写oracle统计查询的时候,遇到listagg聚合函数分组聚合之后出现很多重复数据的问题,于是研究了一下listagg去重的几种方法,以下通过实例讲解三种实现listagg去重的方法。二、方法首先还原listagg聚合之后出现重复数据的现象,打开plsql,执行如下sql:selectt.department_namedepname,…

    2022年9月7日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号