-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimu_tool.m
More file actions
72 lines (54 loc) · 1.8 KB
/
simu_tool.m
File metadata and controls
72 lines (54 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
cd 'c:/users/lab/desktop/simu'
X=load('data_x.txt');
y=load('data_y.txt');
m=length(y);
J_of_predict=zeros(3,1);
fprintf('ploting data ... \n');
fprintf('start try linear regression with single factor x \n');
X=[ones(m,1),X];
n = size(X,2);
initial_theta=zeros(n,1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
% Print theta to screen
J_of_predict(1)=cost;
predict_curve(cost,theta,X,y);hold on
title('predicted linear regression function \n');
%{
initial_theta=zeros(n,1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
%}
fprintf('start trying polynomial fuction up to power 4 \n');
X=load('data_x.txt');
% try polynomial function up x^4
degree=4;
% find polynomial function power which
% fit well with training set
[cost,theta,X_poly,power]=best_poly(X,y,degree);
J_of_predict(2)=cost;
predict_curve(cost,theta,X_poly,y);hold on
str=['predicted polynomial fuction with X power:',num2str(power)];
title(str);
X=load('data_x.txt');
[exp_X]=expFeature(X);
exp_X=[ones(m,1),exp_X];
n = size(exp_X,2);
initial_theta=zeros(n,1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, exp_X, y)), initial_theta, options);
% Print theta to screen
J_of_predict(3)=cost;
predict_curve(cost,theta,exp_X,y);hold on
title('predicted Exponential function \n');
fprintf('cost of three_predict_functions : \n');
fprintf(' %f \n', J_of_predict);