02_roots_and_optimization
Repository for homework 2
Question 2
cat_cable = @(T) -35+T/10.*cosh(10./T*30)+30-T/10;
[root,fx,ea,iter]=falsepos(cat_cable,900,910,0.00001)
cat_cable = @(T) -35+T/10.*cosh(10./T*30)+30-T/10;
[root,fx,ea,iter]=bisect(cat_cable,908,909,0.00001)
cat_cable = @(T) -35+T/10.*cosh(10./T*30)+30-T/10;
[root,ea,iter]=mod_secant(cat_cable,900,910,0.00001)
| solver | initial guess | ea | number of iterations|
| --- | --- | --- | --- |
|falsepos | 900, 910 | 0.00001| 4 |
|mod_secant | 900, 910 | 0.00001| 50 |
|bisect | 900, 910 | 0.00001| 17 |
Question 3
f = @(x) (x-1)*exp(-(x-1)^2);
df = @(x) -(2*(x)^2-4*x+1)*exp(-(x-1)^2);
[root,ea,iter]=newtraph(f,df,3,.00001,5)
### divergence of Newton-Raphson method
| iteration | x_i | approx error |
| --- | --- | --- |
| 0 | 3 | n/a |
| 1 | 3 | 8.6957 |
| 2 | 3 | 6.8573 |
| 3 | 3 | 5.7348 |
| 4 | 3 | 4.9605 |
| 5 | 3 | 4.3873 |
### convergence of Newton-Raphson method
| iteration | x_i | approx error |
| --- | --- | --- |
| 0 | 1.2 | n/a |
| 1 | 1.2 | 22.1239 |
| 2 | 1.2 | 1.7402 |
| 3 | 1.2 | 0.0011 |
| 4 | 1.2 | 2.3315e-13 |
| 5 | 1.2 | 2.3315e-13 |
Question 4
a)
lj = @(x) 4*0.039*((2.394./x).^12-(2.394./x).^6);
[x,E,ea,its] = goldmin(lj,0,3)
x = 2.6872
E = -0.0390
ea = 9.7092e-05
its = 27
b)
F = (0:0.0022/30:.0022)
lj = @(x) 4*0.039*((2.394./x).^12-(2.394./x).^6);
ET = @(dx) lj(dx)*(2.6872+dx)- F*dx;
[x,fx,ea,iter]=goldmin(ET,0,1)
x = 1.0000
fx = 1.0e+04
ea = 8.6968e-05
iter = 27
c)
function SSE = sse_of_parabola(K,xdata,ydata)
% calculate the sum of squares error for a parabola given a function, func, and xdata and ydata
% output is SSE=sum of squares error
K1=K(1);
K2=K(2);
y_function = K1*xdata+1/2*K2*xdata.^2;
SSE = sum((ydata-y_function).^2);
end
d)
dx = zeros(1,50); % [in nm]
F_applied=linspace(0,0.0022,50)
[K,SSE_min]=fminsearch(@(K) sse_of_parabola(K,dx,F_applied),[1,1])
K = 1 1
SSE_min = 8.1490e-05
e)