f



Least Square Fit

I tried to use lsqlin to do a least square fit with 3 constraints. I
had 7 variables. However it didn't seem to take into account the last
2 cosntraints.
I am trying to use fmincon but keep getting an error 'Inner matrix
dimensions must agree'- I checked the dimensions of the constraint
and Obj function matrices.but Ican't figure out why this is
happening.
Help!
0
sprabhu (5)
7/14/2004 7:13:53 PM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
357 Views

Similar Articles

[PageSpeed] 29

In article <eee31fe.-1@webx.raydaftYaTP>,
 "Shar P" <sprabhu@ieor.berkeley.edu> writes:
 >I tried to use lsqlin to do a least square fit with 3 constraints. I
 >had 7 variables. However it didn't seem to take into account the last
 >2 cosntraints.
 >I am trying to use fmincon but keep getting an error 'Inner matrix
 >dimensions must agree'- I checked the dimensions of the constraint
 >and Obj function matrices.but Ican't figure out why this is
 >happening.
 >Help!
post your code here , maybe you have a simple coding error the 
matrices describing the constraints.
peter
0
7/15/2004 11:25:03 AM
inst_s_a_2 tries to fit a 6 degree polynomial in time to fit the
positions given by c.trjectory{ind}(:,3). c is a data structure that
I input to the polynomial.
I am also using the function finidng coefficients to find C,d,A,B
before calling lsqlin.
Thanks for your help.. both the function are pasted below

function [new_traj, e]=find_inst_s_a_2(c,id)
ay=[];

%Find the index of the vehicle id in the data stucture
ind=find(c.id==id);
new_traj=[];

%Loop to find the acceleration & vel for each point in the vehicle
%trajectory except for 1st four and last four

for i=4:length(c.trajectory{ind}(1:end-4,1))
    
    %time_ind is the index of that particular point in the vehicle
    %trajectory i.e. the i-th observation in that trajectory
    
    time_ind=i;
    
    %Window Size=7
    window_inds=[i-3:i+3];

    %Find the 1 by 7 matrix X_t- y coordinates of all the points in
the
    %window
    X_t=[];
    for j=1:length(window_inds)
        X_t=[X_t, c.trajectory{ind}(window_inds(j),3)];
    end

    %Find the 7 by 7 matrix T_t
    T_t=[];
    for k=1:length(window_inds)
        T_t(k,:)=[1 window_inds(k) window_inds(k)^2 window_inds(k)^3
window_inds(k)^4 window_inds(k)^5 window_inds(k)^6];
    end
       
    %Find the matrices C,d,A,b requiered for the lsqlin function
    [C,d,A,b]= finding_coeffs(X_t,T_t);
    
    %Perform Least Square Fit
    options=optimset('Largescale','off','TolCon',[0,0,0]);
    [beta]=lsqlin(C,d,A,b,[],[],[],[],[],options);
    new_ay=T_t(4,:)*beta;
    ay=[ay; new_ay];
    %Append the vel & accel to the new_traj matrix
    new_traj=[new_traj;-A(1,:)*beta,A(2,:)*beta];
end

__________
function [C,d,A,b]= finding_coeffs(X_t,T_t)

%FInd the C matrix- if you're not sure if I have taken weights into
account
%correctly- you can run it by setting w=[1 1 1 1 1 1 1]. This will be
%exactly the same as the 7 by 7 T_t matrix
for f=1:length(T_t(:,1))
    C(f,:)=T_t(f,:);
end

%Find the d matrix- again if you wish you can set w=[1 1 1 1 1 1 1].
This
%will be exactly the same as the 1 by 7 X_t matix
for g=1:length(T_t(:,1))
    d(g)=X_t(g);
end

%A and b are the constraint matrices: A*beta <=b
%First row of A&b take into account :speed>=0;
%Second row: Accel <=5/225
%Third row: Accel >= -5/225

A=[0,-1,-2*T_t(4,2),-3*T_t(4,3),-4*T_t(4,4),-5*T_t(4,5),-6*T_t(4,6);
   0,0,2,6*T_t(4,2),12*T_t(4,3),20*T_t(4,4),30*T_t(4,5);
0,0,-2,-6*T_t(4,2),-12*T_t(4,3),-20*T_t(4,4),-30*T_t(4,5)];
b=[0; 7/225; 7/225];

THANKS!!

Peter Spellucci wrote:
>
>
>
> In article <eee31fe.-1@webx.raydaftYaTP>,
> "Shar P" <sprabhu@ieor.berkeley.edu> writes:
> >I tried to use lsqlin to do a least square fit with 3
> constraints. I
> >had 7 variables. However it didn't seem to take into account
the
> last
> >2 cosntraints.
> >I am trying to use fmincon but keep getting an error 'Inner
> matrix
> >dimensions must agree'- I checked the dimensions of the
> constraint
> >and Obj function matrices.but Ican't figure out why this is
> >happening.
> >Help!
> post your code here , maybe you have a simple coding error the
> matrices describing the constraints.
> peter
>
0
sprabhu (5)
7/15/2004 6:19:54 PM
Reply: