Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

QUES 3B END TERM EXAM

//Find the root of x^3-10x^2+3x=0 that lies in the interval of 1 to 2


//also plot the function
clc
n=100;
y=zeros(n)
x=0:0.01:1
root=zeros(2)
j=1;
function y=f(x)
y=x^3-10*x^2+3*x
endfunction
for i=1:length(x)
if((abs(f(x(i)))<0.1)&&(abs(f(x(i+1)))>=0.1))
root(j)=x(i);
j=j+1;
end
end
disp(root)
plot(x,f(x))
plot(root,f(root),'k.')
xlabel('x','fontsize',3)
ylabel('y(x)','fontsize',3)
legend('y(x)','roots of y(x) between 1 and 2 is '+string(root))
title('Plot of y(x) and its root ','fontsize',3)
QUES 6 END TERM EXAM
/ Solve the 2nd order BVP defined by the ode d2y/dx2 + dy/dx + y = COS(3x)
// y(0) = 1 y(5) =-1
// using bisection method
function D_y=f(x, y)
D_y(1) = y(2)
D_y(2) = COS(3*x)- y(1)- y(2)
endfunction
x = linspace(0,5,100)
10
alpha_1 =-1e4
alpha_2 = 1e7
tolerance = 1e-5
gap = abs(alpha_1- alpha_2)
i=1
while gap > tolerance
//disp("while")
y0 = [1;alpha_1]
solution = ode(y0,0,x,f)
y_b = solution(1,$)
difference_1 = y_b- (-1)
//disp('diff 1 is',difference_1)
y0 = [1;alpha_2]
solution = ode(y0,0,x,f)
y_b = solution(1,$)
difference_2 = y_b- (-1)
//disp('diff 2 is',difference_2)
if (difference_1*difference_2) > 0
disp("choose proper interval")
break
end
alpha_mid = (alpha_1+alpha_2)/2
y0 = [1;alpha_mid]
solution = ode(y0,0,x,f)
y_b = solution(1,$)
difference_mid = y_b- (-1)
//disp('diff mid is',difference_mid)
//disp(difference_1*difference_mid)
if difference_1*difference_mid < 0 then
alpha_2 = alpha_mid
//disp("lower")
else
end
alpha_1 = alpha_mid
//disp('upper')
gap = abs(alpha_1- alpha_2)
disp('gap is',gap)
disp(i)
i=i+1
//disp('alpha_mid is',alpha_mid)
end
y0= [1;alpha_mid]
final_solution = ode(y0,0,x,f)
y_final = final_solution(1,:)
plot(x,y_final)
plot([0,5],[-1,-1],'k')

You might also like