Commit 37efff51 authored by GARLAPATI SREEKAR's avatar GARLAPATI SREEKAR

First Lab added

parents
Citations:
https://www.gnu.org/software/octave/doc/v4.0.0/Simple-File-I_002fO.html //textread
http://homepages.math.uic.edu/~hanson/Octave/OctaveNonlinearEG.html //example for fsolve and fzero
http://stackoverflow.com/questions/12932052/what-is-the-fastest-way-to-write-a-matrix-to-a-text-file-in-octave // has an example for file i/o
Group member info:
Team 16 : Invictus
(Sreekar Garlapati ,150050065) , (Nagendra Reddy,150050067), (Sai Prakash , 150050075)
Honor Code:
We pledge on our honour that we have not given or received any unauthorized assistance on this assignment .
Percentage : 150050065 :100% , 150050067 : 100% , 150050075 : 100%
\ No newline at end of file
The example nicely illustrates the basic use of GNU octave's I/O system and also the use of functions and how to solve non linear equations.
\ No newline at end of file
xx=fsolve(@test,pi/4);// storing output in a variable
file_id = fopen('output inlab task 01.txt', 'w+');//getting write file id
fprintf(file_id, "%f",xx);//writing to the file
fclose(file_id);//closing file id
\ No newline at end of file
function y=test(x)
[a]=textread('input_inlab_task_A1.txt',"%f");//reading the input file
c=a(1,1);//storing in
d=a(2,1);//two variables as required
y=c*cos(x)*sin(x/3)/sin(x)+d*sin(x/3)-1;// the function whose zero is to be computed
endfunction
\ No newline at end of file
Group Info:
Team Invictus (16)
(Sreekar Garlapati,150050065),(Nagendra Reddy,150050067),(Sai Prakash,150050075)
Reflection Essay:
This set of problems teaches us to pass and return matrices to functions in octave and also highlights a few impressive applications of matrix multiplication .
Citations :
https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Vectors_and_matrices // Introduces matrices well, with a few useful functions
https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Writing_functions // Has a fairly detailed documentation of functions in octave
Honor code:
We pledge on our honour that we have not given or received any unauthorised on this assignment or the previous one.
Percentage:
150050065:100%
150050067:100%
150050075:100%
0 0 0
1 0 0
0 0 1
\ No newline at end of file
function [final] = final_state_3(init,action)
final=zeros(3);
A =[1,1,0,1,0,0,0,0,0; #conversion matrix
1,1,1,0,1,0,0,0,0;
0,1,1,0,0,1,0,0,0;
1,0,0,1,1,0,1,0,0;
0,1,0,1,1,1,0,1,0;
0,0,1,0,1,1,0,0,1;
0,0,0,1,0,0,1,1,0;
0,0,0,0,1,0,1,1,1;
0,0,0,0,0,1,0,1,1];
ini=zeros(9,1); #temporary row matrix for storing init values
act=zeros(9,1); #temporary row matrix for storing action values
fin=zeros(9,1); #temporary row matrix for storing final matrix values
for i=1:3
for j=1:3
ini(3*(i-1)+j,1)=init(i,j); #converting 3x3 into 9x1 matrix
act(3*(i-1)+j,1)=action(i,j);
endfor
endfor
fin=(A*act)+ini;
fin=mod(fin,2); #forming the final matrix in 9x1 format
for i=1:3
for j=1:3
final(i,j)=fin(3*(i-1)+j,1); #conversion to 3x3
endfor
endfor
endfunction
\ No newline at end of file
1 1 0
0 0 1
0 0 0
\ No newline at end of file
function [action] = lookup_solve_3(initial)
action = zeros(3);
action(2,:)=initial(1,:); #clearing the first row
final = final_state_3(initial,action); #finding the resulting matrix
action(3,:)=final(2,:); #clearing the second row
final = final_state_3(initial,action); #finding the resulting matrix
check = zeros(3);
if final==check
else
action(1,:) = self_change(final(3,:)); #changing first row according to the last row
final = final_state_3(initial,action);
action = action+lookup_solve_3(final); #repitition by recurrsion
action = mod(action,2);
endif
endfunction
\ No newline at end of file
A=zeros(3);
[N]=textread('input.txt',"%f");
for i=1:3
for j=1:3
A(i,j)=N(3*(i-1)+j);
endfor
endfor
#initial = [1,1,0;1,1,1;0,0,1]
action = @lookup_solve_3(A)
\ No newline at end of file
A=zeros(3);
B=zeros(3);
[N]=textread('input.txt',"%f");
for i=1:3
for j=1:3
A(i,j)=N(3*(i-1)+j);
endfor
endfor
[N]=textread('action.txt',"%f");
for i=1:3
for j=1:3
B(i,j)=N(3*(i-1)+j);
endfor
endfor
final = @final_state_3(A,B)
\ No newline at end of file
function [final] = self_change(action) #function forming the correspondence shown in the lookup table
final = zeros(1,3);
A = [1,1,0;1,1,1;0,1,1]; #conversion matrix for the mapping
final = mod(action*A,2);
endfunction
#the conversion using the lookup table can be conveniently done using matrix multiplication , in particular , multiplying the last row with A
# gives the top row buttons to be pressed.
\ No newline at end of file
Group Info:
Team Invictus (16)
(Sreekar Garlapati,150050065),(Nagendra Reddy,150050067),(Sai Prakash,150050075)
Reflection Essay:
Task A:
This task had the equation solving capability taken to the next level.The problem requires a careful analysis of the
situation and the answer can go wrong if one isn't careful enough.
Task A1 was relatively straightforward and had laid the foundation for the next task.
Task A2 was a tad tougher and the equation does get a bit messy. This checks scrupulous analysis on the solver's part.
Task B:
This was an extension of the inlab assignment, with the generalization of the earlier solution.One very interesting observation
which is incredibly crucial to this solution is that the conversion matrices have an odd determinant.The solution wouldn't have been
possible if this weren't so. Besides this observation , the problem has a good application of matrix inversion technique and also
careful analysis of the situation ( as it will always be in our SSL assignments :-) )
Citations :
http://www.ueda.info.waseda.ac.jp/~n-kato/lightsout/ // the given link for testing various outputs for task B
New resources were not needed in attempting task A
Honor code:
We pledge on our honour that we have not given or received any unauthorised on this assignment or the previous one.
Percentage:
150050065:100%
150050067:100%
150050075:100%
function [fin]=back_conv(C,m,n) #function to convert the column matrix back into the correct form
fin=zeros(m,n);#initializing empty array
if rows(C)*columns(C)==m*n #running a check
else
disp("error");
return;
endif
for i=1:m
for j=1:n
fin(i,j)=C(n*(i-1)+j,1); #entering the values into the m X n matrix
endfor
endfor
endfunction
\ No newline at end of file
function [root]=backward_solve(input) #the main function which computes the answer
root=zeros(rows(input),columns(input)); #an initial zero matrix of the same size as input
A=get_conversion_matrix(input); #the get_conversion_matrix call which gives us the matrix used to convert action vector to effect vector
B=col_conv(input); #converting input into col matrix
x=det(A)*inv(A)*B; #finding out the answer in column matirx form
root=back_conv(x,rows(input),columns(input)); #converting the answer column back into an m X n matrix form
root=int32(root); #removing the ugly decimals
root=mod(root,2); #returns the root
endfunction
\ No newline at end of file
function [root]=backward_solve_3(input) #basically the backward_solve function but with a check for the input size
if rows(input)==3
else return # this block performs the check
endif
if columns(input)==3
else return
endif
root=zeros(rows(input),columns(input));
A=get_conversion_matrix(input);
B=col_conv(input);
x=det(A)*inv(A)*B;
root=back_conv(x,rows(input),columns(input));
root=int32(root);
root=mod(root,2);
endfunction
\ No newline at end of file
function [colm]=col_conv(mat) #converts into a column matrix
m=rows(mat);
n=columns(mat); #store the size
rowm=zeros(m*n,1);
for i=1:m
for j=1:n
colm(n*(i-1)+j,1)=mat(i,j); #the colm vector which is returned is storing the elements of the input in these loops
endfor
endfor
endfunction
\ No newline at end of file
function res= eqn(theta)
[N]=textread("input_outlab_task_A1.txt","%f");
x1=N(1);
y1=N(2);
d=N(3);
w=N(4); #all these lines store
n=N(5); #the input values
res=(y1-w)*tan(theta)+w*tan(get_alpha(theta,n))-x1; #the equation needed to be solved for theta
# the get_alpha function calculates the angle of refraction in the smoke screen
endfunction
\ No newline at end of file
function res=eqn2(theta)
[N]=textread("input_outlab_task_A2.txt","%f");
x0=N(1);
y0=N(2);
x1=N(3);
y1=N(4);
v2=N(5);
vl=N(6);
d=N(7);
w=N(8);
n=N(9);
t1=N(10);
x1ground=x1;
y1ground=y1;
x1=x1ground-x0; # the input reading code lines
y1=y1ground-y0; # with the values x1 , y1 set to the relative values and the originals are stored in the ground suffixed values
res = ((x1-(w*tan(get_alpha(theta,n))))/(vl*sin(theta))+((w*n)/(vl*cos(get_alpha(theta,n)))))-((((x1-(w*tan(get_alpha(theta,n) ) ) )*cot(theta))-y1+w-(v2*t1))/v2);
# the above equation is the exact equation for getting the value of theta
endfunction
\ No newline at end of file
function ans=final_return() # a straightforward file with the purpose of returning the values of the various scattered functions
theta=fsolve(@eqn2,pi/4); # gets the value of theta
[x2,y2]=get_y(theta); # gets the final coordinates
ans(1)=theta*180/pi;
ans(2)=x2;
ans(3)=y2; #answer stores the values
endfunction
\ No newline at end of file
function alpha = get_alpha (theta,n)
alpha=asin(sin(theta)/n); # getting value of refraction inside smoke screen
endfunction
\ No newline at end of file
function [conv]=get_conversion_matrix(input) #this forms the conversion matrix
m=rows(input);
n=columns(input);
conv=zeros(m*n);
for i=1:m
for j=1:n
conv((n*(i-1)+j),(n*(i-1)+j))=1; #(i,i) is set to 1
if i==1
else
conv((n*(i-1)+j),(n*(i-2)+j))=1; #the element on the top of it is set to 1 in the conversion matrix if it isnt in the first row
endif
if i==m
else
conv((n*(i-1)+j),(n*(i)+j))=1; #the element on the bottom of it is set to 1 in the conversion matrix if it isnt in the last row
endif
if j==1
else
conv((n*(i-1)+j),(n*(i-1)+j-1))=1; #the element on the left of it is set to 1 in the conversion matrix if it isnt in the leftmost column
endif
if j==n
else
conv((n*(i-1)+j),(n*(i-1)+j+1))=1; #the element on the right of it is set to 1 in the conversion matrix if it isnt in the rightmost column
endif
endfor
endfor
endfunction
\ No newline at end of file
function [xfin,yfin]=get_y(theta)
[N]=textread("input_outlab_task_A2.txt","%f");
x0=N(1);
y0=N(2);
x1=N(3);
y1=N(4);
v2=N(5);
vl=N(6);
d=N(7);
w=N(8);
n=N(9);
t1=N(10);
x1ground=x1;
y1ground=y1;
x1=x1ground-x0;
y1=y1ground-y0; # same as before , gets the relative coordinates
xfin=x1;
yfin=(((((x1-(w*tan(get_alpha(theta,n) ) ) )*cot(theta))-y1+w-(v2*t1))/v2)+t1)*v2+y1; #finds the final y coordinate (relative)
endfunction
\ No newline at end of file
180.0 100 0.05 5 3.5
\ No newline at end of file
0.0 0.0 76.63 27 0.027 0.0555 7 5 3.5 5
\ No newline at end of file
ans=fsolve("eqn",pi/4)*180/pi; #getting the value of theta for A1
file_id=fopen("output_outlab_task_A1.txt",'w+'); #getting file id
fprintf(file_id,"%f",ans); #writing theta
fclose(file_id); #closing file id
\ No newline at end of file
ans=final_return();
file_id=fopen("output_outlab_task_A2.txt",'w+'); #getting file id
fprintf(file_id,"%f",ans(1)); #writing theta
fprintf(file_id,"%s"," ");
fprintf(file_id,"%f",ans(2)); #writing x final
fprintf(file_id,"%s"," ");
fprintf(file_id,"%f",ans(3)); #writing y final
fclose(file_id); #closing file id
\ No newline at end of file
62.003645
\ No newline at end of file
41.070191 76.630000 91.838433
\ No newline at end of file
A=[ 1 0 1 ;
1 1 1 ;
1 1 1 ;]; #sample test case
backward_solve(A) #running the general task B2
disp("\n");
backward_solve_3(A) #running taskB1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment