# Regression analysis for enthalpy vaporization

5 views (last 30 days)

Show older comments

### Answers (2)

Image Analyst
on 22 Nov 2022

You can use nlmfit() to find the A and n. Attached is a demo. Adapt as needed.

If you need more help, then attach your data as text, not an image. No one wants to type in all that from the image. You should do that to make it easier for people to help you.

##### 0 Comments

Image Analyst
on 23 Nov 2022

OK, I took your data and plugged it into my demo, and altered the formula a bit to accomodate subtracting x from Tc, and I get this:

% Uses fitnlm() to fit a non-linear model (a power law curve) through noisy data.

% Requires the Statistics and Machine Learning Toolbox, which is where fitnlm() is contained.

% Initialization steps.

clc; % Clear the command window.

close all; % Close all figures (except those of imtool.)

clear; % Erase all existing variables. Or clearvars if you want.

workspace; % Make sure the workspace panel is showing.

format long g;

format compact;

fontSize = 20;

T = [90 120 150 200 240 270 300 340];

H = [2.46 2.33 2.21 2.01 1.83 1.67 1.46 1.05];

Tc = 369.83;

% Create the X and Y coordinates.

X = T;

Y = H;

% Now we have noisy training data that we can send to fitnlm().

% Plot the noisy initial data.

plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 20);

grid on;

% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.

tbl = table(X', Y');

% Define the model as Y = a * (x .^ b) + c

% Note how this "x" of modelfun is related to big X and big Y.

% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.

modelfun = @(b,x) b(1) * (Tc - x(:, 1)) .^ b(2) + b(3);

beta0 = [0.3, 0.4, 0.005]; % Guess values to start with. Just make your best guess. They don't have to match the [a,b,c] values from above because normally you would not know those.

% Now the next line is where the actual model computation is done.

mdl = fitnlm(tbl, modelfun, beta0);

% Now the model creation is done and the coefficients have been determined.

% YAY!!!!

% Extract the coefficient values from the the model object.

% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.

coefficients = mdl.Coefficients{:, 'Estimate'}

% Create smoothed/regressed data using the model:

yFitted = coefficients(1) * (Tc - X) .^ coefficients(2) + coefficients(3);

% Do another fit but for a lot more points, including points in between the training points.

X1000 = linspace(X(1), X(end), 1000);

yFitted1000 = coefficients(1) * (Tc - X1000) .^ coefficients(2) + coefficients(3);

% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.

hold on;

% Plot red diamonds fitted values at the training X values.

plot(X, yFitted, 'rd', 'LineWidth', 2, 'MarkerSize', 10);

% Plot fitted values at all the 1000 X values with a red line.

plot(X1000, yFitted1000, 'r-', 'LineWidth', 2);

grid on;

title('Power Law Regression with fitnlm()', 'FontSize', fontSize);

xlabel('T', 'FontSize', fontSize);

ylabel('H', 'FontSize', fontSize);

legendHandle = legend('Original, Training H', 'Fitted Y at training X', 'Fitted Y everywhere', 'Location', 'north');

legendHandle.FontSize = 25;

message = sprintf('Coefficients for H = a * (Tc - T) ^ b + c:\n a = %8.5f\n b = %8.5f\n c = %8.5f',...

coefficients(1), coefficients(2), coefficients(3));

text(75, 1.5, message, 'FontSize', 23, 'Color', 'r', 'FontWeight', 'bold', 'Interpreter', 'none');

% Set up figure properties:

% Enlarge figure to full screen.

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

% Get rid of tool bar and pulldown menus that are along top of figure.

% set(gcf, 'Toolbar', 'none', 'Menu', 'none');

% Give a name to the title bar.

set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

coefficients =

0.294217681695381

0.374922637220179

0.0054348738254875

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!