Sei sulla pagina 1di 3

function any = coil(n)

%WILMER S. SARMIENTO
Z=zeros(n);
Z(1,1:n)=1:n;
Z(n,1:n-1)=3*n-2:-1:2*n;
Z(2:n,n)=n+1:2*n-1;
Z(2:n-1,1)=4*n-4:-1:3*n-1; %R1 D2 L3 U4
for dir=1:4
switch dir
case 1
col=n;
movc=n-1;
movr=n;
f=4*n-4;
for row=2:ceil(n/2)
col=col-1;
movc=movc-2;
movr=movr-2;
Z(row,row:col)=f+1:f+movr;
f=f+movr-2;
f=f+2*(movc)+movr;
end
case 2
f=5*n-6;
movc=n-2;
movr=n-2;
cox=n;
row=2;
for col=n-2:-1:ceil(n/2+1)
movr=movr-2;
movc=movc-2;
cox=cox-1;row=row+1;
Z(row:col,cox)=f+1:f+movr;
f=f+movr-2;
f=f+2*(movc)+2*movr;
end
case 3
col=n;
movc=n-1;
movr=n;
f=6*n-9;
rox=n;
for row=2:ceil(n/2)
col=col-1;
movc=movc-2;
movr=movr-2;
rox=rox-1;
Z(rox,row:col)=f+movr-1:-1:f;
f=f+movr-1;
f=f+2*(movc);
end
case 4
cox=n-1;row=2;
movr=n-2;
movc=n;
f=7*n-12;
for col=2:ceil(n/2-1)
row=row+1;cox=cox-1;
movr=movr-2;movc=movc-2;
Z(row:cox,col)=f+movr:-1:f+1;
f=f+movr-6;
f=f+2*movc+movr;
end
end
any=Z;
end
end

function any = snake( n )


%WILMER SARMIENTO
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
R=reshape(1:(n*n),n,n)';
W=R;
for row=2:2:n
W(row,:)=fliplr(W(row,:));
end
any=W;
end

function answer = rootf( f,v,n )


%WILMER S. SARMIENTO
sc=length(v);%used to determine what method to use...
if sc>1%condition
method=1;% bisection method
else method=2;% newton rhapson
end
n=ceil(n);
switch method
case 1%computing the root by using bisection method
a=v(1,1); b=v(1,2);
xa=a; xb=b;
fa=f(a);
fb=f(b);
xk=(xa+xb)/2;
fx=f(xk);
x(1)=xk;
row=1;
nsig(1)=-1;
while nsig(row)<n
row=row+1;
if sign(fa)==sign(fx)
xa=xk; fa=fx;
elseif sign(fb)==sign(fx)
xb=xk; fb=fx;
end
xk=(xa+xb)/2;
fx=f(xk);
x(row)=xk;
error=abs(x(row)-x(row-1))/x(row)*100;
nsig(row)=floor(1-log10(0.2*error));
dataerror(row)=error;
end
fprintf('using bisection method see the difference between root and
error');
answer=[x;dataerror]';%display the root of equation(x) and the percent
error(dataerror)
case 2% computing the root by using newton rhapson
syms x;
v(1)=v;%input v
fp=diff(f,x);%differentiate the function
for index=1:n%iterations
v(index+1)=v(index)-f(v(index))/eval(subs(fp,v(index)));% formula
A(index)=v(index);% colects data in every loop
end
fprintf('by using newton-rhapson method');
answer=A'; %displays the answer
end

Potrebbero piacerti anche