12 views (last 30 days)

Image Analyst
on 30 Dec 2019

or essentially I = a * exp(b * V) + c, then the intersection of the slope line depends on where the slope is evaluated. This is because the exponential function f( x) = e x has the special property that its derivative is the function itself, f′( x) = e x = f( x). So, do you want the slope at the last x that you have provided in your text file? If so, the slope is pretty easy to compute analytically. It's slope = a * b * exp(b * V). So use fitnlm() to figure out a, b, and c (using attached demo), then plug in a, b, and V to get the slope at that point. Then, knowing the slope and using the point-slope version of the equation for a line passing through that right-most point, you can get the equation of the line, then set that equal to 0 and solve for V.

Let me know if you want a fully worked out solution.

Image Analyst
on 30 Dec 2019

See this:

% Uses fitnlm() to fit a non-linear model (an exponential decay 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;

filename = fullfile(pwd, 'shottky_simr1.txt')

strData = importdata(filename)

X = strData.data(:, 1);

Y = strData.data(:, 2);

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

% Plot the noisy initial data.

plot(X, Y, 'b.', 'LineWidth', 2, 'MarkerSize', 15);

grid on;

hold on

beta0 = [0.01, -0.0000001, -0.1]; % Guess values to start with. Just make your best guess.

% y = beta0(1) * exp(beta0(1)*X) + beta0(3);

% plot(X, y, 'r-');

% 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 + exp(-b*x)

% 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) * exp(b(2)*x(:, 1)) + b(3);

% 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) * exp(coefficients(2)*X) + 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(X, yFitted, 'r-', 'LineWidth', 2);

grid on;

title('Exponential Regression with fitnlm()', 'FontSize', fontSize);

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

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

legendHandle = legend('Actual Y', 'Fitted Y', 'Location', 'north');

legendHandle.FontSize = 30;

formulaString = sprintf('Y = %.3e * exp(%.3g * X) + %.3g', coefficients(1), coefficients(2), coefficients(3))

yl = ylim;

text(-0.25, mean(yl), formulaString, 'FontSize', 25, 'FontWeight', 'bold', 'Color', 'r');

% Get slope at xMax

[xMax, indexAtXMax] = max(X)

yMax = Y(indexAtXMax)

slope = coefficients(1) * coefficients(2) * exp(coefficients(2) * xMax)

% Point slope formula (y-yMax) = slope * (x-xMax)

% Get the formula for y=0, and x=xMax

% -yMax/slope = (x - xMax)

x0 = -yMax / slope + xMax

% Plot a * there

plot(x0, 0, 'r*', 'MarkerSize', 20, 'LineWidth', 2);

line([x0, xMax], [0, yMax], 'Color', 'r', 'LineWidth', 2);

caption = sprintf('(%3e, 0)', x0);

text(x0, -0.001, caption, 'FontSize', 25, 'FontWeight', 'bold', 'Color', 'r');

% Set up figure properties:

% Enlarge figure to full screen.

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

% 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')

You can see the fit is very good, and the tangent at the last point intersects the x axis at

x0 =

0.235560584047374

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781920

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781920

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781923

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781923

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781927

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781927

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781939

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781939

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781979

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/498476-using-least-squares-to-find-the-x-intercept#comment_781979

Sign in to comment.