clear all; close all; clc;
% read data;
opts = detectImportOptions('Training_Data.xlsx',...
    'sheet','ITBO');
T = readtable('Training_Data.xlsx',...
    opts)
%% input parameters to the welding machine
power_ = 100;
speed_ = 1000;
TH_ = 1;
% other parameters ...

test_parameter = [power_./speed_; TH_];

%% https://matlabacademy.mathworks.com/details/matlab-onramp/gettingstarted
%% Plot Dependecies
% fig = figure(1); hold on
% % fig.WindowState = 'maximized';
% tiledlayout(2,3)
% 
% nexttile(1)
% plot(T.Power,T.Strength, '*')
% xlabel('Power in Watt')
% ylabel('Strength in MPa')
% 
% nexttile(2)
% plot(T.Speed, T.Strength, '*')
% xlabel('Speed in mm/sec')
% ylabel('Strength in MPa')
% 
% nexttile(3)
% plot(T.Pressure, T.Strength, '*')
% xlabel('Pressure in MPa')
% ylabel('Strength in MPa')
% 
% nexttile(4)
% plot(T.Weld_Seam_Area, T.Strength, '*')
% xlabel('Area in mm^2')
% ylabel('Strength in MPa')
% 
% nexttile(5)
% plot(T.TH, T.Strength, '*')
% xlabel('Thickness in mm')
% ylabel('Strength in MPa')
% 
% nexttile(6)
% plot(T.Power./T.Speed./T.Weld_Seam_Area,T.Strength, 'rx');
% xlabel('Energy in watt')
% ylabel('Strength in MPa')

%% gradient descent
X = [T.Power./T.Speed, T.TH]; y = T.Strength;
X = featureNormalize(X);
m = length(X); % number of training examples
X = [ones(m,1),X]; % Add a column of ones to x
theta = zeros(width(X),1);% initialize fitting parameters
% theta = [0 100 50]';
iterations = 100;
alpha = 0.1;
J = computeCost(X, y, theta);
[theta,theta_hist,J_hist] = gradientDescent(X, y, theta, alpha, iterations);
theta

figure;
plot(J_hist);

test_answer = theta'*[1;test_parameter]

% theta0_vals = linspace(0, 35, 100);
% theta1_vals = linspace(0, 10, 100);

%% normal equation
X = [T.Power./T.Speed, T.TH];
y = T.Strength;
m = length(y);

% Add intercept term to X
X = [ones(m, 1) X];

% Calculate the parameters from the normal equation
theta = inv(X'*X)*X'*y
test_answer = theta'*[1;test_parameter]

%% Matlab linear learner
linMdl = trainedModel.LinearModel;
theta = linMdl.Coefficients.Estimate;
predict(linMdl,test_parameter')

%%
