-1

I have been trying to get a solution to 16 variables with 16 equations (linear) with the solve command.

Below I am pasting my code

syms x11 x12 x13 x14 x21 x22 x23 x24 x31 x32 x33 x34 x41 x42 x43 x44;
 %defining symbolic variables

x=[x11 x12 x13 x14; x21 x22 x23 x24; x31 x32 x33 x34; x41 x42 x43 x44]; % putting all those variables in matrix form

N4=[-1 3 -3 1;3 -6 3 0;-3 3 0 0;1 0 0 0]; % 4*4 control matrix 
digits(4);% setting precision to 4 digits instead of default 32 
syms sx;% defining a variable sx 
for i=1:8 
    for j=1:8 
        u=(i-1)/7; 
        w=(i-1)/7; 
        sx(i,j)= [u^3 u^2 u 1]*N4*x*N4'[w^3;w^2;w;1]; % this gives me 8*8 matrix
    end
end

for a=1:8 
    for b=1:8 
        kx(a,b)=b; % the x coordinates of 64 points of the image 
    end
end

ex=0 % defining variable ex which is the error function. 

for i=1:8 
    for j=1:8 
        ex= ex+ (sx(i,j)-kx(i,j))^2;
    end
end

diff_x11=vpa(diff(ex,x11)); % I want to find values of x11,x12... 
diff_x12=vpa(diff(ex,x12)); % for which the error ex is minimum
diff_x13=vpa(diff(ex,x13)); % so I differentiate ex with all 16 
diff_x14=vpa(diff(ex,x14)); % variables and get 16 equations
diff_x21=vpa(diff(ex,x21));% and then try to solve them 
diff_x22=vpa(diff(ex,x22)); 
diff_x23=vpa(diff(ex,x23)); 
diff_x24=vpa(diff(ex,x24)); 
diff_x31=vpa(diff(ex,x31)); 
diff_x32=vpa(diff(ex,x32)); 
diff_x33=vpa(diff(ex,x33)); 
diff_x34=vpa(diff(ex,x34)); 
diff_x41=vpa(diff(ex,x41)); 
diff_x42=vpa(diff(ex,x42)); 
diff_x43=vpa(diff(ex,x43)); 
diff_x44=vpa(diff(ex,x44)); 
    qx=solve(diff_x11,diff_x12,diff_x13,diff_x14,diff_x21,diff_x22,diff_x23,diff_x24,diff_x31,diff_x32,diff_x33,diff_x34,diff_x41,diff_x42,diff_x43,diff_x44,x11,x12,x13,x14,x21,x22,x23,x24,x31,x32,x33,x34,x41,x42,x43,x44);

I used the solve command as shown above. Initially I was getting only 2 values to variables x11 and x44. that time the precision was default 32 digits. As I reduced the precision and started using the 'vpa' command as shown in the code I got two more values x22 and x33. Finally I am getting solution to only 4 variables out of 16 and all 4 of them are diagonal elements of 4*4 matrix with variable elements

I need to get all 16 of them. Any help will be greatly appreciated. Thanks Ankit

ankitm511
  • 1
  • 1
  • 2
  • 3
    Please format code as `code` using the back-tick or by highlighting the code and selecting the `{}` icon. – PengOne Jul 10 '11 at 04:37
  • 1
    @ankitm511: please only post relevant code. the part dealing with images in the beginning is not related to the equations later on.. – Amro Jul 10 '11 at 15:36

1 Answers1

0

If you're solving linear equations, why do you do it in such a complicated fashion?

For example, if you want to solve the system of linear equations

Ax+By=C
Dx+Ey=F
Gx+Hy=I

i.e. you want to know the numerical values [x;y] that best (in a least-square sense) fit the three equations, you write

%# A,B, etc are numbers, or arbitrarily complex functions
%# that evaluate to a numerical value at the time the equations are solved. 
matrixA = [A,B;D,E;G,H];
matrixB = [C;F;I];

xyVector = matrixA \ matrixB;
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • @Amro : Ok...The image part is being used in the next part of the code which is used later. Anyways I have edited it. – ankitm511 Jul 11 '11 at 03:33
  • jonas: Thanks for ur suggestion. But here I dont know the coefficients A,B,C,D etc. All the equations are derived by differentiating a single equation 16 times partially with 16 different variables. MATLAB on its own finds the variables in an expression and solves for them when I used the 'solve' command – ankitm511 Jul 11 '11 at 03:36
  • @ankitm511: For least-squares optimization, you don't need to differentiate. You write down the equation and then use the linear or nonlinear solver to find the parameter values that minimize the error. Can you write down the original equation in some easily readable form? – Jonas Jul 11 '11 at 13:39