使用优化工具箱是时问题。
15 views (last 30 days)
Show older comments
我用matlab的优化app来求解一个非线性最小二乘法问题主函数:
close all; clear; clc;
x=-3:1:3
y=-2:1:2
[X,Y]= meshgrid(x,y)
Z=X.^2+Y.^2
mesh(X,Y,Z)
hold on
d=1/180*pi
e=2/180*pi
f=3/180*pi
T=[1,0,0,0
0,1,0,0
0,0,1,0
0.1,0.2,0.3,1]
R1=[1,0,0,0
0,cos(d),sin(d),0
0,(-1)*sin(d),cos(d),0
0,0,0,1];
R2=[cos(e),0,(-1)*sin(e),0
0,1,0,0
sin(e),0,cos(e),0
0,0,0,1];
R3=[cos(f),sin(f),0,0
(-1)*sin(f),cos(f),0,0
0,0,1,0
0,0,0,1];
t=size(X,1)*size(X,2);
W=[X(:),Y(:),Z(:),ones(t,1)]*T*R1*R2*R3;
global X2
global Y2
global Z2
X2=W(:,1)
Y2=W(:,2)
Z2=W(:,3)
plot3(X2,Y2,Z2)
函数文件:
function [n,x1,x2,x3] = obj(a,x,y,z)
%UNTITLa(5)a(4)4 此处显示有关此函数的摘要
% 此处显示详细说明
T=[1,0,0,0
0,1,0,0
0,0,1,0
a(1),a(2),a(3),1];
R1=[1,0,0,0
0,cos(a(4)),sin(a(4)),0
0,(-1)*sin(a(4)),cos(a(4)),0
0,0,0,1];
R2=[cos(a(5)),0,(-1)*sin(a(5)),0
0,1,0,0
sin(a(5)),0,cos(a(5)),0
0,0,0,1];
R3=[cos(a(6)),sin(a(6)),0,0
(-1)*sin(a(6)),cos(a(6)),0,0
0,0,1,0
0,0,0,1];
w=[x,y,z,1]*R3*R2*R1*T;
x1=w(:,1);
x2=w(:,2);
x3=w(:,3);
n=x3-x1^2-x2^2;
使用优化app时的报错如图:我检查过没有上述说的括号的问题,实在不知道怎么回事了,请赐教!

0 Comments
Accepted Answer
lapep
on 18 Nov 2022
你调用方式不太对,我修改了一下你的代码,但是有warning,选择合适的算法求吧:)
obj.M
function [n,x1,x2,x3] = obj(t)
%UNTITLa(5)a(4)4 此处显示有关此函数的摘要
% 此处显示详细说明
x=t(1);
y=t(2);
a1=t(3);
a2=t(4);
a3=t(5);
d=t(6);
e=t(7);
f=t(8);
[X,Y]= meshgrid(x,y);
z=X.^2+Y.^2;
T=[1,0,0,0
0,1,0,0
0,0,1,0
a1,a2,a3,1];
R1=[1,0,0,0
0,cos(d),sin(d),0
0,(-1)*sin(d),cos(d),0
0,0,0,1];
R2=[cos(e),0,(-1)*sin(e),0
0,1,0,0
sin(e),0,cos(e),0
0,0,0,1];
R3=[cos(f),sin(f),0,0
(-1)*sin(f),cos(f),0,0
0,0,1,0
0,0,0,1];
w=[x,y,z,1]*R3*R2*R1*T;
x1=w(:,1);
x2=w(:,2);
x3=w(:,3);
n=x3-x1^2-x2^2;
Optimization running.
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
Objective function value: 7.888609052210118E-31
Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the function tolerance.
0 Comments
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!