From: alok gautam on
Dear Friends,

i am using fsolver in matlab when i write my all equation manually then i get correct answer. If i write these equations with help of for loop i am not getting correct ans.
will any one please help me.

i am new in matlab


thanks in advance


Alok
x0=[-0.1085 -0.3293 -0.3618 -0.3293 -0.1085 0 0 0 0 0 0 0 0 0 0 0
-0.3293 0 0 0 -0.3293 0 0 0 0 0 0 0 0 0 0 0
-0.3618 0 0 0 -0.3618 0 0 0 0 0 0 0 0 0 0 0
-0.3293 0 0 0 -0.3295 0 0 0 0 0 0 0 0 0 0 0
-0.1085 -0.3294 -0.363 -0.338 -0.11 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 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 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
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 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 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 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 0 0 0 0 0 0 0 0 0 0 0 0];
options=optimset('Display', 'iter');
[x,fval]=fsolve(@myfun,x0,options)


my function
function F=myfun(x)
h=2;
fhi=(pi*60/180);
a=((x(2,1)-2*x(1,1)+x(1,1))/(h^2));
b=((x(1,2)-2*x(1,1)+x(1,1))/(h^2));
c=((x(2,1)-x(1,1))/(2*h));
d=((x(1,2)-x(1,1))/(2*h));
e=(((x(2,1)-h*tan(fhi))-x(1,2)+x(1,1)-x(2,1))/(4*(h^2)));
f(1)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(1,1)*((1+(c^2)+(d^2))^1.5);


a=(((x(2,1)-h*tan(fhi))-2*x(1,2)+x(1,2))/(h^2));
b=((x(1,3)-2*x(1,2)+x(1,1))/(h^2));
c=(((x(2,1)-h*tan(fhi))-x(1,2))/(2*h));
d=((x(1,3)-x(1,1))/(2*h));
e=(((x(1,3)-h*tan(fhi))-x(1,3)+x(1,1)-x(2,1))/(4*(h^2)));
f(2)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(1,2)*((1+(c^2)+(d^2))^1.5);


a=(((x(1,3)-h*tan(fhi))-2*x(1,3)+x(1,3))/(h^2));
b=((x(1,4)-2*x(1,3)+x(1,2))/(h^2));
c=(((x(1,3)-h*tan(fhi))-x(1,3))/(2*h));
d=((x(1,4)-x(1,2))/(2*h));
e=(((x(2,5)-h*tan(fhi))-x(1,4)+x(1,2)-(x(2,1)-h*tan(fhi)))/(4*(h^2)));
f(3)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(1,3)*((1+(c^2)+(d^2))^1.5);


a=(((x(2,5)-h*tan(fhi))-2*x(1,4)+x(1,4))/(h^2));
b=((x(1,5)-2*x(1,4)+x(1,3))/(h^2));
c=(((x(2,5)-h*tan(fhi))-x(1,4))/(2*h));
d=((x(1,5)-x(1,3))/(2*h));
e=((x(2,5)-x(1,5)+x(1,3)-(x(1,3)-h*tan(fhi)))/(4*(h^2)));
f(4)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(1,4)*((1+(c^2)+(d^2))^1.5);


a=((x(2,5)-2*x(1,5)+x(1,5))/(h^2));
b=((x(1,5)-2*x(1,5)+x(1,4))/(h^2));
c=((x(2,5)-x(1,5))/(2*h));
d=((x(1,5)-x(1,4))/(2*h));
e=((x(2,5)-x(1,5)+x(1,4)-(x(2,5)-h*tan(fhi)))/(4*(h^2)));
f(5)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(1,5)*((1+(c^2)+(d^2))^1.5);


a=((x(3,1)-2*x(2,1)+x(1,1))/(h^2));
b=(((x(2,1)-h*tan(fhi))-2*x(2,1)+x(2,1))/(h^2));
c=((x(3,1)-x(1,1))/(2*h));
d=(((x(2,1)-h*tan(fhi))-x(2,1))/(2*h));
e=(((x(3,1)-h*tan(fhi))-x(1,2)+x(1,1)-x(3,1))/(4*(h^2)));
f(6)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(2,1)*((1+(c^2)+(d^2))^1.5);


a=((x(3,5)-2*x(2,5)+x(1,5))/(h^2));
b=((x(2,5)-2*x(2,5)+(x(2,5)-h*tan(fhi)))/(h^2));
c=((x(3,5)-x(1,5))/(2*h));
d=((x(2,5)-(x(2,5)-h*tan(fhi)))/(2*h));
e=((x(3,5)-x(1,5)+x(1,4)-(x(3,5)-h*tan(fhi)))/(4*(h^2)));
f(7)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(2,5)*((1+(c^2)+(d^2))^1.5);

a=((x(4,1)-2*x(3,1)+x(2,1))/(h^2));
b=(((x(3,1)-h*tan(fhi))-2*x(3,1)+x(3,1))/(h^2));
c=((x(4,1)-x(2,1))/(2*h));
d=(((x(3,1)-h*tan(fhi))-x(3,1))/(2*h));
e=(((x(4,1)-h*tan(fhi))-(x(2,1)-h*tan(fhi))+x(2,1)-x(4,1))/(4*(h^2)));
f(8)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(3,1)*((1+(c^2)+(d^2))^1.5);


a=((x(4,5)-2*x(3,5)+x(2,5))/(h^2));
b=((x(3,5)-2*x(3,5)+(x(3,5)-h*tan(fhi)))/(h^2));
c=((x(4,5)-x(2,5))/(2*h));
d=((x(3,5)-(x(3,5)-h*tan(fhi)))/(2*h));
e=((x(4,5)-x(2,5)+(x(2,5)-h*tan(fhi))-(x(4,5)-h*tan(fhi)))/(4*(h^2)));
f(9)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(3,5)*((1+(c^2)+(d^2))^1.5);

a=((x(5,1)-2*x(4,1)+x(3,1))/(h^2));
b=(((x(4,1)-h*tan(fhi))-2*x(4,1)+x(4,1))/(h^2));
c=((x(5,1)-x(3,1))/(2*h));
d=(((x(4,1)-h*tan(fhi))-x(4,1))/(2*h));
e=((x(5,2)-(x(3,1)-h*tan(fhi))+x(3,1)-x(5,1))/(4*(h^2)));
f(10)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(4,1)*((1+(c^2)+(d^2))^1.5);


a=((x(5,5)-2*x(4,5)+x(3,5))/(h^2));
b=((x(4,5)-2*x(4,5)+(x(4,5)-h*tan(fhi)))/(h^2));
c=((x(5,5)-x(3,5))/(2*h));
d=((x(4,5)-(x(4,5)-h*tan(fhi)))/(2*h));
e=((x(5,5)-x(3,5)+(x(3,5)-h*tan(fhi))-x(5,4))/(4*(h^2)));
f(11)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(4,5)*((1+(c^2)+(d^2))^1.5);

a=((x(5,1)-2*x(5,1)+x(4,1))/(h^2));
b=((x(5,2)-2*x(5,1)+x(5,1))/(h^2));
c=((x(5,1)-x(4,1))/(2*h));
d=((x(5,2)-x(5,1))/(2*h));
e=((x(5,2)-(x(4,1)-h*tan(fhi))+x(4,1)-x(5,1))/(4*(h^2)));
f(12)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(5,1)*((1+(c^2)+(d^2))^1.5);


a=((x(5,2)-2*x(5,2)+(x(4,1)-h*tan(fhi)))/(h^2));
b=((x(5,3)-2*x(5,2)+x(5,1))/(h^2));
c=((x(5,2)-(x(4,1)-h*tan(fhi)))/(2*h));
d=((x(5,3)-x(5,1))/(2*h));
e=((x(5,3)-(x(5,3)-h*tan(fhi))+x(4,1)-x(5,1))/(4*(h^2)));
f(13)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(5,2)*((1+(c^2)+(d^2))^1.5);

a=((x(5,3)-2*x(5,3)+(x(5,3)-h*tan(fhi)))/(h^2));
b=((x(5,4)-2*x(5,3)+x(5,2))/(h^2));
c=((x(5,3)-(x(5,3)-h*tan(fhi)))/(2*h));
d=((x(5,4)-x(5,2))/(2*h));
e=((x(5,4)-(x(4,5)-h*tan(fhi))+(x(4,1)-h*tan(fhi))-x(5,2))/(4*(h^2)));
f(14)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(5,3)*((1+(c^2)+(d^2))^1.5);

a=((x(5,4)-2*x(5,4)+(x(4,5)-h*tan(fhi)))/(h^2));
b=((x(5,5)-2*x(5,4)+x(5,3))/(h^2));
c=((x(5,4)-(x(4,5)-h*tan(fhi)))/(2*h));
d=((x(5,5)-x(5,3))/(2*h));
e=((x(5,5)-x(4,5)+x(4,3)-x(5,3))/(4*(h^2)));
f(15)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(5,4)*((1+(c^2)+(d^2))^1.5);

a=((x(5,5)-2*x(5,5)+x(4,5))/(h^2));
b=((x(5,5)-2*x(5,5)+x(5,4))/(h^2));
c=((x(5,5)-x(4,5))/(2*h));
d=((x(5,5)-x(5,4))/(2*h));
e=((x(5,5)-x(4,5)+(x(4,5)-h*tan(fhi))-x(5,4))/(4*(h^2)));
f(16)=a*(1+(d^2))+b*(1+(c^2))-2*c*d*e-x(5,5)*((1+(c^2)+(d^2))^1.5);

F=[f(1);f(2);f(3);f(4);f(5);f(6);f(7);f(8);f(9);f(10);f(11);f(12);f(13);f(14);f(15);f(16)];



correct ans
x =

Columns 1 through 15

-0.1085 -0.3293 -0.3618 -0.3293 -0.1085 0 0 0 0 0 0 0 0 0 0
-0.3293 -0.0000 0 0 -0.3293 0 0 0 0 0 0 0 0 0 0
-0.3618 -0.0000 0 0 -0.3618 0 0 0 0 0 0 0 0 0 0
-0.3293 -0.0000 0.0000 0 -0.3295 0 0 0 0 0 0 0 0 0 0
-0.1085 -0.3294 -0.3630 -0.3380 -0.1100 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
0.0000 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 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 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 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 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 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Column 16

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
for loop program



function F=myfun(x)
h=2;
fhi=(pi*60/180);

for i=2
for j=2:6
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j-1)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end


for i=3
for j=2
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+4)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end



for i=3
for j=6
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+1)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end




for i=4
for j=2
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+6)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end



for i=4
for j=6
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+3)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end



for i=5
for j=2
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+8)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end



for i=5
for j=6
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+5)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end


for i=6
for j=2:6
a1=((x(i+1,j)-2*x(i,j)+x(i-1,j))/(h^2));
b1=((x(i,j+1)-2*x(i,j)+x(i,j-1))/(h^2));
c1=((x(i+1,j)-x(i-1,j))/(2*h));
d1=((x(i,j+1)-x(i,j-1))/(2*h));
e1=((x(i+1,j+1)-x(i-1,j+1)+x(i-1,j-1)-x(i+1,j-1))/(4*(h^2)));
f(j+10)=((1+(d1^2))*a1+(1+(c1^2))*b1-2*c1*d1*e1-x(i,j)*((1+(c1^2)+(d1^2))^(3/2)));
end
end

if (1i==1)
for j=1:7
x(1i,j)=x(1i+1,j);
end
x(1i+1,1)=x(1i+1,2)
x(1i+1,7)=x(1i+1,6)
end

if (1i==3)
for j=1
x(1i,j)=x(1i,j+1);
end
x(7,j+1)=x(6,j+1)
end
if (1i==4)
for j=1
x(1i,j)=x(1i,j+1);
end
x(7,j+1)=x(6,j+1)
end
if (1i==5)
for j=1
x(1i,j)=x(1i,j+1);
end
x(7,j+1)=x(6,j+1)
end
if (1i==6)
for j=1
x(1i,j)=x(1i,j+1);
end
x(7,j+1)=x(6,j+1)
end
if (1i==7)
for j=1
x(1i,j)=x(1i,j+1);
end
x(7,j+1)=x(6,j+1)
end

if (1i==7)
for j=3:7
x(1i,j)=x(1i-1,j);
end
x(1i-1,7)=x(1i-1,6)
end

if (1i==3)
for j=7
x(1i,j)=x(1i,j-1);
end
end

if (1i==4)
for j=7
x(1i,j)=x(1i,j-1);
end
end

if (1i==5)
for j=7
x(1i,j)=x(1i,j-1);
end
end

if 1i==3
for j=3
x(1i,j)=x(1i,j-1)-h*tan(fhi);
end
end

if 1i==3
for j=4
x(1i,j)=x(1i-1,j)-h*tan(fhi);
end
end


if 1i==3
for j=5
x(1i,j)=x(1i,j+1)-h*tan(fhi);
end
end




if 1i==4
for j=3
x(1i,j)=x(1i,j-1)-h*tan(fhi);
end
end



if 1i==4
for j=5
x(1i,j)=x(1i,j+1)-h*tan(fhi);
end
end



if 1i==5
for j=3
x(1i,j)=x(1i,j-1)-h*tan(fhi);
end
end


if 1i==5
for j=4
x(1i,j)=x(1i+1,j)-h*tan(fhi);
end
end

if 1i==5
for j=5
x(1i,j)=x(1i,j+1)-h*tan(fhi);
end
end


F=[f(1);f(2);f(3);f(4);f(5);f(6);f(7);f(8);f(9);f(10);f(11);f(12);f(13);f(14);f(15);f(16)];




Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
> In fsolve at 324
In root at 20

First-Order Norm of
Iteration Func-count Residual optimality Lambda step
0 257 0.735809 1.15 0.01
1 514 1.15391e-005 0.00334 0.001 0.409482
2 771 8.24804e-013 9.31e-007 0.0001 0.00167442
3 1028 8.7778e-022 2.96e-011 1e-005 5.08957e-007
Optimization terminated: the first-order optimality measure is less than 1e-4 times options.TolFun.

x =

Columns 1 through 15

-0.1085 -0.3150 -0.3521 -0.3249 -0.1424 -0.0009 0.0000 -0.0000 0 0 0 0 0 0 0
-0.3150 -0.0935 -0.0600 -0.0566 -0.0746 -0.0164 -0.0009 0.0000 0.0000 0 0 0 0 0 0
-0.3521 -0.0600 0.0193 0.0043 -0.3829 -0.0545 0.0076 -0.0000 0 0 0 0 0 0 0
-0.3249 -0.0567 0.0043 -0.0000 -0.3221 -0.0483 0.0074 0.0000 0 0 0 0 0 0 0
-0.1424 -0.0747 -0.3840 -0.3305 -0.1030 -0.0190 0.0035 -0.0000 0 0 0 0 0 0 0
-0.0009 -0.0164 -0.0548 -0.0493 -0.0191 -0.0044 0.0014 0 0 0 0 0 0 0 0
0.0000 -0.0009 0.0076 0.0075 0.0035 0.0014 -0.0000 0.0000 0 0 0 0 0 0 0
0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0 0 0 0 0 0 0
-0.0000 -0.0000 0 0 -0.0000 -0.0000 0.0000 -0.0000 0 0 0 0 0 0 0
0.0000 0.0000 -0.0000 -0.0000 0 -0.0000 0.0000 0.0000 0 0 0 0 0 0 0
-0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0 0 0 0 0 0 0
0.0000 0.0000 0.0000 0 -0.0000 -0.0000 0.0000 0.0000 0 0 0 0 0 0 0
0.0000 -0.0000 -0.0000 0 0 0.0000 -0.0000 -0.0000 0 0 0 0 0 0 0
0.0000 -0.0000 0 0 0.0000 0.0000 0.0000 -0.0000 0 0 0 0 0 0 0
0.0000 -0.0000 0 0.0000 0 -0.0000 -0.0000 -0.0000 0 0 0 0 0 0 0
-0.0000 0.0000 0 0 0 0 0.0000 -0.0000 0 0 0 0 0 0 0

Column 16

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0


fval =

1.0e-010 *

-0.1775
-0.1221
-0.0914
0.0485
0.0168
-0.1221
-0.0174
-0.0914
-0.0322
0.0485
-0.0260
0.0167
-0.0177
-0.0326
-0.0262
-0.0207
 | 
Pages: 1
Prev: SELF ORGANIZED MAP
Next: Sobel Edge Detection