Non-Linear Models to Data

I have made a Linear model for my data using this code, I can't figure out how to create a non linear regression model which predicts the compressive strength of concrete based on its composition and age, how do I do this?:
% Load data from the file
data = readmatrix('Concrete_Data.xls');
% Define predictor variables and response variable
predictors = data(:, 1:end-1);
response = data(:, end);
% Create a random partition of the data into 70% training data and 30% testing data
rng(1); % Set the random number generator seed for reproducibility
cv = cvpartition(length(data), 'HoldOut', 0.3);
% Split the data into training and testing sets using the partition indices
training_data = data(cv.training,:);
testing_data = data(cv.test,:);
% Create a linear regression model using the training data
model = fitlm(training_data(:,1:end-1), training_data(:,end));
disp(model)
% Displaying of the graph
figure;
plot(model)
% Calculate the R-squared score of the model using the testing data
y_pred = predict(model, testing_data(:,1:end-1));
R2 = 1 - sum((testing_data(:,end) - y_pred).^2) / sum((testing_data(:,end) - mean(testing_data(:,end))).^2);
% Output the results to the command window
fprintf('R-squared score obtained with the linear model using %d variables: %.2f\n', size(predictors, 2), R2);

2 Comments

I guess you are taking the same class as Sam.
Take a look that question and my comments and answer for some ideas I shared.
Thankyou

Sign in to comment.

Answers (0)

Products

Release

R2022a

Tags

Asked:

on 3 May 2023

Commented:

on 4 May 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!