Skip to content
Permalink
73d6f8c923
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
282 lines (207 sloc) 5.59 KB
# curve_fitting
##Problem 1
###Function least_squares.m
```matlab
function [ a, fx, r2 ] = least_squares( Z, y )
%Function calculates the coefficients for a best fit function with least
%squares techniques
% a = coeficients
% fx = line of best fit
% r2 = coefficient of determination
% evaluate coefficients for a polynomial of 3 degrees
a = Z\y; % evaluates constants for function
fx = Z*a; % evaluates function
Sr = sum((y-Z*a).^2);
r2 = 1-Sr/sum((y-mean(y)).^2); % gives r^2 value
end
```
###Evaluated for case one yields
```matlab
Z = [x.^0, x, 1./x];
>> [ a, fx, r2 ] = least_squares( Z, y )
a =
0.3745
0.9864
0.8456
fx =
2.2066
2.7702
3.6157
4.5317
5.4758
r2 =
0.9996
```
###Evaluated for case two yields
```matlab
>> Z = [x.^0, x, x.^2, x.^3]
>> [ a, fx, r2 ] = least_squares( Z, y )
a =
-11.4887
7.1438
-1.0412
0.0467
fx =
1.8321
3.4145
4.0347
3.5087
2.9227
2.4947
3.2330
4.9595
r2 =
0.8290
```
###Evaluated for case three yields
```matlab
>> Z = [exp(-1.5*x), exp(-.3*x),exp(-.05*x)];
>> [ a, fx, r2 ] = least_squares( Z, y )
a =
4.0046
2.9213
1.5647
fx =
5.9321
4.5461
3.2184
2.5789
2.1709
1.8726
1.6425
1.4605
1.1940
r2 =
0.9971
```
___
##Problem 2
###Function cost_logistic.m
``` matlab
function [J, grad] = cost_logistic(a, x, y)
% cost_logistic Compute cost and gradient for logistic regression
% J = cost_logistic(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.
% Initialize some useful values
N = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of a.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
t = a(1)+a(2).*x;
sigm = 1./(1+exp(-t));
J = sum(-y.*log(sigm)- (1-y).*log(1-sigm));
costFun = @ (a) sum(-y.*log((1./(1+exp(-(a(1)+a(2).*x)))))- (1-y).*log(1-(1./(1+exp(-(a(1)+a(2).*x))))));
grad = (1/length(x))*sum((sigm - y).*t);
initial_a = [0 0];
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = fminunc(costFun, initial_a);
tt = theta(1)+theta(2).*x;
sigmm = 1./(1+exp(-tt));
setdefaults
plot(x,y,'o', x, sigmm);
title('Best fit regression model')
xlabel('Temp (F)')
ylabel('Pass/Fail (0,1)')
% Note: grad should have the same dimensions as theta
% =============================================================
end
```
###Function evaluated
```matlab
>> [J, grad] = cost_logistic(a, x, y)
J =
115.5085
grad =
5.0130
```
###Plot of best fit linear regression
![plot](https://github.uconn.edu/github-enterprise-assets/0000/1383/0000/0401/4264d71a-1eab-11e7-8fc4-445afe720517.jpg)!
#Problem 3
The following function was created to evalaute problem 3
```matlab
function sigma_z=boussinesq_lookup(q,a,b,z)
% function that determines stress under corner of an a by b rectangular platform
% z-meters below the platform. The calculated solutions are in the fmn data
% m=fmn(:,1)
% in column 2, fmn(:,2), n=1.2
% in column 3, fmn(:,2), n=1.4
% in column 4, fmn(:,2), n=1.6
fmn= [0.1,0.02926,0.03007,0.03058
0.2,0.05733,0.05894,0.05994
0.3,0.08323,0.08561,0.08709
0.4,0.10631,0.10941,0.11135
0.5,0.12626,0.13003,0.13241
0.6,0.14309,0.14749,0.15027
0.7,0.15703,0.16199,0.16515
0.8,0.16843,0.17389,0.17739];
m=a/z;
n=b/z;
% find which n row to use
nn = [ 1.2 1.4 1.6 ];
r_n = round(n,1);
n_loc = min(abs(nn - r_n));
row_n = find( abs(nn - r_n) == n_loc)+1; % sets row for n
% find which m row to use
mm = fmn(:,1)';
r_m = round(m,1);
m_loc = min(abs(mm - r_m));
row_m = find( abs(mm - r_m) == m_loc); % sets row for m
m_values = fmn(row_m-2:row_m+1, 1);
n_values = fmn(row_m-2:row_m+1, row_n);
pcon = polyfit(m_values', n_values', 3);
x = m_values';
func = @(x) pcon(4) + pcon(3)*x + pcon(2)*x.^2 + pcon(1)*x.^3;
y = func(x);
func(m);
sigma_z = q*func(m);
end
```
##Spline Interpolation
```matlab
function sigma_z=boussinesq_spline(q,a,b,z)
% function that determines stress under corner of an a by b rectangular platform
% z-meters below the platform. The calculated solutions are in the fmn data
% m=fmn(:,1)
% in column 2, fmn(:,2), n=1.2
% in column 3, fmn(:,2), n=1.4
% in column 4, fmn(:,2), n=1.6
fmn= [0.1,0.02926,0.03007,0.03058
0.2,0.05733,0.05894,0.05994
0.3,0.08323,0.08561,0.08709
0.4,0.10631,0.10941,0.11135
0.5,0.12626,0.13003,0.13241
0.6,0.14309,0.14749,0.15027
0.7,0.15703,0.16199,0.16515
0.8,0.16843,0.17389,0.17739];
m=a/z;
n=b/z;
% find which n row to use
nn = [ 1.2 1.4 1.6 ];
r_n = round(n,1);
n_loc = min(abs(nn - r_n));
row_n = find( abs(nn - r_n) == n_loc)+1; % sets row for n
% find which m row to use
mm = fmn(:,1)';
r_m = round(m,1);
m_loc = min(abs(mm - r_m));
row_m = find( abs(mm - r_m) == m_loc); % sets row for m
m_values = fmn(row_m-2:row_m+1, 1);
n_values = fmn(row_m-2:row_m+1, row_n);
pcon = interp3(m_values', n_values', 'cubic');
x = m_values';
func = @(x) pcon(4) + pcon(3)*x + pcon(2)*x.^2 + pcon(1)*x.^3;
y = func(x);
func(m);
plot(x,y)
sigma_z = q*func(m);
end
```
#END