Nasser Abbasi
Output
» clear all;
» nma_HW_4_24_newtn
nma_HW_4_24_newtn - Program to solve a system of nonlinear equations
using Newton's method. Equations defined by function fnewt.
» nma_HW_4_24_newtn
nma_HW_4_24_newtn - Program to solve a system of nonlinear equations
using Newton's method. Equations defined by function fnewt.
Enter the initial guess (row vector): [0 0.1981]
Enter number of step to iterate:10
Enter name of your fnewt M file to be called:'nma_HW_4_24_fnewt'
k2
now =0.2, position now is x=0.00109735 m, y=0.197022 m
k2
now =0.3, position now is x=0.00217112 m, y=0.195964 m
k2
now =0.4, position now is x=0.00322194 m, y=0.194926 m
k2
now =0.5, position now is x=0.00425044 m, y=0.193906 m
k2
now =0.6, position now is x=0.00525723 m, y=0.192906 m
k2
now =0.7, position now is x=0.00624288 m, y=0.191923 m
k2
now =0.8, position now is x=0.00720797 m, y=0.190958 m
k2
now =0.9, position now is x=0.00815304 m, y=0.19001 m
k2
now =1, position now is x=0.00907862 m, y=0.189079 m
k2
now =1.1, position now is x=0.00998521 m, y=0.188164 m
k2
now =1.2, position now is x=0.0108733 m, y=0.187265 m
k2
now =1.3, position now is x=0.0117434 m, y=0.186382 m
k2
now =1.4, position now is x=0.012596 m, y=0.185514 m
k2
now =1.5, position now is x=0.0134314 m, y=0.18466 m
k2
now =1.6, position now is x=0.0142503 m, y=0.183821 m
k2
now =1.7, position now is x=0.0150528 m, y=0.182997 m
k2
now =1.8, position now is x=0.0158396 m, y=0.182186 m
k2
now =1.9, position now is x=0.0166109 m, y=0.181388 m
k2
now =2, position now is x=0.0173672 m, y=0.180604 m
k2
now =2.1, position now is x=0.0181088 m, y=0.179832 m
k2
now =2.2, position now is x=0.0188362 m, y=0.179073 m
k2
now =2.3, position now is x=0.0195495 m, y=0.178326 m
k2
now =2.4, position now is x=0.0202493 m, y=0.177592 m
k2
now =2.5, position now is x=0.0209358 m, y=0.176869 m
k2
now =2.6, position now is x=0.0216093 m, y=0.176157 m
k2
now =2.7, position now is x=0.0222702 m, y=0.175457 m
k2
now =2.8, position now is x=0.0229188 m, y=0.174768 m
k2
now =2.9, position now is x=0.0235553 m, y=0.174089 m
k2
now =3, position now is x=0.0241801 m, y=0.173421 m
k2
now =3.1, position now is x=0.0247934 m, y=0.172764 m
k2
now =3.2, position now is x=0.0253955 m, y=0.172116 m
k2
now =3.3, position now is x=0.0259867 m, y=0.171478 m
k2
now =3.4, position now is x=0.0265671 m, y=0.17085 m
k2
now =3.5, position now is x=0.0271372 m, y=0.170232 m
k2
now =3.6, position now is x=0.027697 m, y=0.169622 m
k2
now =3.7, position now is x=0.0282469 m, y=0.169022 m
k2
now =3.8, position now is x=0.0287871 m, y=0.168431 m
k2
now =3.9, position now is x=0.0293178 m, y=0.167848 m
k2
now =4, position now is x=0.0298392 m, y=0.167274 m
k2
now =4.1, position now is x=0.0303515 m, y=0.166708 m
k2
now =4.2, position now is x=0.0308549 m, y=0.16615 m
k2
now =4.3, position now is x=0.0313497 m, y=0.165601 m
k2
now =4.4, position now is x=0.031836 m, y=0.165059 m
k2
now =4.5, position now is x=0.032314 m, y=0.164525 m
k2
now =4.6, position now is x=0.0327839 m, y=0.163999 m
k2
now =4.7, position now is x=0.0332459 m, y=0.16348 m
k2
now =4.8, position now is x=0.0337002 m, y=0.162968 m
k2
now =4.9, position now is x=0.0341469 m, y=0.162464 m
k2
now =5, position now is x=0.0345861 m, y=0.161966 m
k2
now =5.1, position now is x=0.0350182 m, y=0.161476 m
k2
now =5.2, position now is x=0.0354431 m, y=0.160992 m
k2
now =5.3, position now is x=0.0358611 m, y=0.160514 m
k2
now =5.4, position now is x=0.0362723 m, y=0.160043 m
k2
now =5.5, position now is x=0.0366768 m, y=0.159579 m
k2
now =5.6, position now is x=0.0370749 m, y=0.159121 m
k2
now =5.7, position now is x=0.0374666 m, y=0.158669 m
k2
now =5.8, position now is x=0.0378521 m, y=0.158223 m
k2
now =5.9, position now is x=0.0382314 m, y=0.157783 m
k2
now =6, position now is x=0.0386048 m, y=0.157348 m
k2
now =6.1, position now is x=0.0389724 m, y=0.15692 m
k2
now =6.2, position now is x=0.0393342 m, y=0.156497 m
k2
now =6.3, position now is x=0.0396905 m, y=0.156079 m
k2
now =6.4, position now is x=0.0400412 m, y=0.155667 m
k2 now
=6.5, position now is x=0.0403866 m, y=0.15526 m
k2
now =6.6, position now is x=0.0407267 m, y=0.154859 m
k2
now =6.7, position now is x=0.0410616 m, y=0.154462 m
k2
now =6.8, position now is x=0.0413915 m, y=0.154071 m
k2
now =6.9, position now is x=0.0417164 m, y=0.153684 m
k2
now =7, position now is x=0.0420365 m, y=0.153302 m
k2
now =7.1, position now is x=0.0423519 m, y=0.152925 m
k2
now =7.2, position now is x=0.0426625 m, y=0.152553 m
k2
now =7.3, position now is x=0.0429686 m, y=0.152185 m
k2
now =7.4, position now is x=0.0432702 m, y=0.151822 m
k2
now =7.5, position now is x=0.0435675 m, y=0.151464 m
k2
now =7.6, position now is x=0.0438604 m, y=0.151109 m
k2
now =7.7, position now is x=0.0441491 m, y=0.150759 m
k2
now =7.8, position now is x=0.0444337 m, y=0.150413 m
k2
now =7.9, position now is x=0.0447142 m, y=0.150072 m
k2
now =8, position now is x=0.0449908 m, y=0.149734 m
k2
now =8.1, position now is x=0.0452634 m, y=0.149401 m
k2
now =8.2, position now is x=0.0455322 m, y=0.149071 m
k2
now =8.3, position now is x=0.0457973 m, y=0.148745 m
k2
now =8.4, position now is x=0.0460586 m, y=0.148424 m
k2
now =8.5, position now is x=0.0463164 m, y=0.148105 m
k2
now =8.6, position now is x=0.0465706 m, y=0.147791 m
k2
now =8.7, position now is x=0.0468213 m, y=0.14748 m
k2
now =8.8, position now is x=0.0470687 m, y=0.147173 m
k2
now =8.9, position now is x=0.0473126 m, y=0.146869 m
k2
now =9, position now is x=0.0475533 m, y=0.146569 m
k2
now =9.1, position now is x=0.0477907 m, y=0.146272 m
k2
now =9.2, position now is x=0.048025 m, y=0.145979 m
k2
now =9.3, position now is x=0.0482562 m, y=0.145689 m
k2
now =9.4, position now is x=0.0484843 m, y=0.145402 m
k2
now =9.5, position now is x=0.0487094 m, y=0.145118 m
k2
now =9.6, position now is x=0.0489316 m, y=0.144838 m
k2
now =9.7, position now is x=0.0491508 m, y=0.14456 m
k2
now =9.8, position now is x=0.0493673 m, y=0.144286 m
k2
now =9.9, position now is x=0.0495809 m, y=0.144014 m
k2
now =10, position now is x=0.0497918 m, y=0.143746 m
k2
now =10.1, position now is x=0.05 m, y=0.143481 m
k2
now =10.2, position now is x=0.0502056 m, y=0.143218 m
k2
now =10.3, position now is x=0.0504085 m, y=0.142958 m
k2
now =10.4, position now is x=0.0506089 m, y=0.142701 m
k2
now =10.5, position now is x=0.0508068 m, y=0.142447 m
k2
now =10.6, position now is x=0.0510022 m, y=0.142195 m
k2
now =10.7, position now is x=0.0511952 m, y=0.141946 m
k2
now =10.8, position now is x=0.0513859 m, y=0.1417 m
k2
now =10.9, position now is x=0.0515741 m, y=0.141456 m
k2
now =11, position now is x=0.0517601 m, y=0.141215 m
k2
now =11.1, position now is x=0.0519438 m, y=0.140976 m
k2
now =11.2, position now is x=0.0521253 m, y=0.14074 m
k2
now =11.3, position now is x=0.0523046 m, y=0.140506 m
k2
now =11.4, position now is x=0.0524818 m, y=0.140275 m
k2
now =11.5, position now is x=0.0526568 m, y=0.140046 m
k2
now =11.6, position now is x=0.0528298 m, y=0.139819 m
k2
now =11.7, position now is x=0.0530007 m, y=0.139595 m
k2
now =11.8, position now is x=0.0531696 m, y=0.139373 m
k2
now =11.9, position now is x=0.0533365 m, y=0.139153 m
k2
now =12, position now is x=0.0535015 m, y=0.138935 m
k2
now =12.1, position now is x=0.0536645 m, y=0.138719 m
k2
now =12.2, position now is x=0.0538257 m, y=0.138506 m
k2
now =12.3, position now is x=0.053985 m, y=0.138295 m
k2
now =12.4, position now is x=0.0541425 m, y=0.138085 m
k2
now =12.5, position now is x=0.0542982 m, y=0.137878 m
k2
now =12.6, position now is x=0.0544522 m, y=0.137673 m
k2
now =12.7, position now is x=0.0546043 m, y=0.13747 m
k2
now =12.8, position now is x=0.0547548 m, y=0.137269 m
k2
now =12.9, position now is x=0.0549036 m, y=0.137069 m
k2
now =13, position now is x=0.0550508 m, y=0.136872 m
k2
now =13.1, position now is x=0.0551963 m, y=0.136677 m
k2
now =13.2, position now is x=0.0553402 m, y=0.136483 m
k2
now =13.3, position now is x=0.0554825 m, y=0.136291 m
k2
now =13.4, position now is x=0.0556233 m, y=0.136101 m
k2
now =13.5, position now is x=0.0557625 m, y=0.135913 m
k2
now =13.6, position now is x=0.0559002 m, y=0.135727 m
k2
now =13.7, position now is x=0.0560365 m, y=0.135542 m
k2
now =13.8, position now is x=0.0561713 m, y=0.135359 m
k2
now =13.9, position now is x=0.0563046 m, y=0.135178 m
k2
now =14, position now is x=0.0564365 m, y=0.134998 m
k2
now =14.1, position now is x=0.056567 m, y=0.13482 m
k2
now =14.2, position now is x=0.0566961 m, y=0.134644 m
k2
now =14.3, position now is x=0.0568239 m, y=0.134469 m
k2
now =14.4, position now is x=0.0569503 m, y=0.134296 m
k2
now =14.5, position now is x=0.0570755 m, y=0.134124 m
k2
now =14.6, position now is x=0.0571993 m, y=0.133954 m
k2
now =14.7, position now is x=0.0573218 m, y=0.133785 m
k2
now =14.8, position now is x=0.0574431 m, y=0.133618 m
k2
now =14.9, position now is x=0.0575632 m, y=0.133453 m
k2
now =15, position now is x=0.057682 m, y=0.133289 m
k2
now =15.1, position now is x=0.0577996 m, y=0.133126 m
k2
now =15.2, position now is x=0.057916 m, y=0.132965 m
k2
now =15.3, position now is x=0.0580312 m, y=0.132805 m
k2
now =15.4, position now is x=0.0581453 m, y=0.132647 m
k2
now =15.5, position now is x=0.0582583 m, y=0.13249 m
k2
now =15.6, position now is x=0.0583701 m, y=0.132334 m
k2
now =15.7, position now is x=0.0584808 m, y=0.13218 m
k2
now =15.8, position now is x=0.0585904 m, y=0.132027 m
k2
now =15.9, position now is x=0.058699 m, y=0.131875 m
k2
now =16, position now is x=0.0588065 m, y=0.131724 m
k2
now =16.1, position now is x=0.0589129 m, y=0.131575 m
k2
now =16.2, position now is x=0.0590183 m, y=0.131427 m
k2
now =16.3, position now is x=0.0591227 m, y=0.131281 m
k2
now =16.4, position now is x=0.0592261 m, y=0.131135 m
k2
now =16.5, position now is x=0.0593285 m, y=0.130991 m
k2
now =16.6, position now is x=0.0594299 m, y=0.130848 m
k2
now =16.7, position now is x=0.0595304 m, y=0.130706 m
k2
now =16.8, position now is x=0.0596299 m, y=0.130566 m
k2
now =16.9, position now is x=0.0597285 m, y=0.130426 m
k2
now =17, position now is x=0.0598261 m, y=0.130288 m
k2
now =17.1, position now is x=0.0599228 m, y=0.130151 m
k2
now =17.2, position now is x=0.0600187 m, y=0.130014 m
k2
now =17.3, position now is x=0.0601136 m, y=0.129879 m
k2
now =17.4, position now is x=0.0602077 m, y=0.129746 m
k2
now =17.5, position now is x=0.0603009 m, y=0.129613 m
k2
now =17.6, position now is x=0.0603932 m, y=0.129481 m
k2
now =17.7, position now is x=0.0604847 m, y=0.12935 m
k2
now =17.8, position now is x=0.0605754 m, y=0.129221 m
k2
now =17.9, position now is x=0.0606652 m, y=0.129092 m
k2
now =18, position now is x=0.0607543 m, y=0.128964 m
k2
now =18.1, position now is x=0.0608425 m, y=0.128838 m
k2
now =18.2, position now is x=0.0609299 m, y=0.128712 m
k2
now =18.3, position now is x=0.0610166 m, y=0.128587 m
k2
now =18.4, position now is x=0.0611025 m, y=0.128464 m
k2
now =18.5, position now is x=0.0611876 m, y=0.128341 m
k2
now =18.6, position now is x=0.061272 m, y=0.128219 m
k2
now =18.7, position now is x=0.0613557 m, y=0.128099 m
k2
now =18.8, position now is x=0.0614386 m, y=0.127979 m
k2
now =18.9, position now is x=0.0615208 m, y=0.12786 m
k2
now =19, position now is x=0.0616023 m, y=0.127742 m
k2
now =19.1, position now is x=0.061683 m, y=0.127624 m
k2
now =19.2, position now is x=0.0617631 m, y=0.127508 m
k2
now =19.3, position now is x=0.0618425 m, y=0.127393 m
k2
now =19.4, position now is x=0.0619212 m, y=0.127278 m
k2
now =19.5, position now is x=0.0619992 m, y=0.127165 m
k2
now =19.6, position now is x=0.0620766 m, y=0.127052 m
k2
now =19.7, position now is x=0.0621534 m, y=0.12694 m
k2
now =19.8, position now is x=0.0622294 m, y=0.126829 m
k2
now =19.9, position now is x=0.0623049 m, y=0.126718 m
k2 now =20, position now is x=0.0623797 m, y=0.126609 m
After 10 iterations the final position is
0.624 0.1266
%
nma_HW_4_24_newtn - Program to solve a system of nonlinear equations
% using
Newton's method. Equations defined by
function fnewt.
clear all; help nma_HW_4_24_newtn; % Clear
memory and print header
%
Modified by Nasser Abbasi for problem 4.24
%* Set
initial guess and parameters
x0 = input('Enter
the initial guess (row vector): ');
x = x0;
% Copy initial guess
xp(:,1) = x(:); % Record initial guess for plotting
nStep = input('Enter
number of step to iterate:');
fnewt = input('Enter
name of your fnewt M file to be called:');
%* Loop
over desired number of steps
%
% start
k2 with small value, increase untill 20 N/m
%
k2= 0.1;
while(
k2<20)
for iStep=1:nStep
%* Evaluate function f and
its Jacobian matrix D
[f D] =
feval(fnewt,x,k2); % fnewt returns value of f and D
%* Find dx by Gaussian elimination
dx = f/D;
%* Update the estimate for
the root
x = x - dx; %
Newton iteration for new x
xp(:,iStep+1) =
x(:); % Save current estimate for plotting
end
k2=k2+0.1;
fprintf('k2 now =%g, position now
is x=%g m, y=%g m\n',k2,x(1),x(2));
%
% notice, last static
equilibrium position is used for next guess
%
xp=[];
xp(:,1) = x(:);
end
%* Print
the final estimate for the root
fprintf('After %g iterations the final position is \n',nStep);
disp(x);
function [f,D] =
nma_HW_4_24_fnewt(state,k2)
%
Function used by the N-variable Newton's method
%
Inputs
% x State vector
[x y]
% param k2
%
Outputs
% f r.h.s. [dx/dt
dy/dt]
% D Jacobian matrix,
D(i,j) = df(j)/dx(i)
% Modified from original fnewtn to
solve problem 4.24
% Nasser abbasi
% Evaluate f(i)
x= state(1);
y= state(2);
f(1) =
10*((x^2+y^2)^(1/2)-1/10)/(x^2+y^2)^(1/2)*x+...
1/20*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-1/10)...
/(100*x^2-20*x+1+100*y^2)^(1/2)*(200*x-20);
f(2) = 10*((x^2+y^2)^(1/2)-1/10)/(x^2+y^2)^(1/2)...
*y+10*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-1/10)/...
(100*x^2-20*x+1+100*y^2)^(1/2)*y-981/1000;
% Evaluate D(i,j)
D(1,1) = 10/(x^2+y^2)*x^2-10*((x^2+y^2)^(1/2)-...
1/10)/(x^2+y^2)^(3/2)*x^2+10*((x^2+y^2)^(1/2)-...
1/10)/(x^2+y^2)^(1/2)+1/400*k2/(100*x^2-20*x+1+100*y^2)*...
(200*x-20)^2-1/40*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-...
1/10)/(100*x^2-20*x+1+100*y^2)^(3/2)*(200*x-20)^2+...
10*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-...
1/10)/(100*x^2-20*x+1+100*y^2)^(1/2);
D(1,2) = 10/(x^2+y^2)*y*x-10*((x^2+y^2)^(1/2)-...
1/10)/(x^2+y^2)^(3/2)*x*y+1/2*k2/(100*x^2-20*x+1+100*y^2)...
*y*(200*x-20)-5*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)...
-1/10)/(100*x^2-20*x+1+100*y^2)^(3/2)*(200*x-20)*y;
D(2,1) =
10/(x^2+y^2)*y*x-10*((x^2+y^2)^(1/2)-1/10)/(x^2+y^2)^(3/2)*x*y+...
1/2*k2/(100*x^2-20*x+1+100*y^2)*y*(200*x-20)-...
5*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-1/10)/(100*x^2-20*x+...
1+100*y^2)^(3/2)*(200*x-20)*y;
D(2,2) =
10/(x^2+y^2)*y^2-10*((x^2+y^2)^(1/2)-1/10)/(x^2+y^2)^(3/2)*y^2+...
10*((x^2+y^2)^(1/2)-1/10)/(x^2+y^2)^(1/2)+100*k2/(100*x^2-20*x+1+...
100*y^2)*y^2-1000*k2*(1/10*(100*x^2-20*x+1+100*y^2)^(1/2)-...
1/10)/(100*x^2-20*x+1+100*y^2)^(3/2)*y^2+10*k2*(1/10*(100*x^2-20*x...
+1+100*y^2)^(1/2)-1/10)/(100*x^2-20*x+1+100*y^2)^(1/2);
return;