Commit 722fcd2a authored by HIMANSHU GUPTA's avatar HIMANSHU GUPTA

add lab1

parents
# Code gives error if no solution in [0,pi/2] exits for given nx,ny.
function y = refraction(i) #defining the function whose roots are to be found
a = load('input_inlab_task_A1.txt'); #taking input from file
nx = a(1);
ny = a(2);
y = sin(i) - (nx*cos(i) + ny*sin(i))*sin(i/3); #function
endfunction
left =0.0001;
#find left,right by iteration
if refraction(pi/2) >0
for n=[0.0001:0.001:pi/2]
if refraction(n)<0
left = n;
break;
end
end
end
if refraction(pi/2) <0
for n=[0.0001:0.001:pi/2]
if refraction(n)>0
left = n;
break;
end
end
end
right = pi/2+0.00001; #defined range for i solution
x0 = [left right];
[i,info] = fzero("refraction", x0); #solving the function in given range
fid = fopen ("output_inlab_task_01.txt", "w"); #writing to text file
fdisp (fid,i);
fclose (fid);
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