How to plot data using the sigmoid and hyperbolic tangent functions.

3 views (last 30 days)
Hello forums;
I am trying to write a MATLAB script to plot some data using a sigmoid and hyperbolic tangent function. I have been researching for so long but to no avail. If somebody could help that would be amazing! The data file and the equations are below. I have some code that has worked somewhat but it is mainly the last function I am struggling with. Anything helps!

Answers (1)

David Hill
David Hill on 21 Oct 2022
Here is your code with a few corrections.
x = xlsread('Lab 4 Data.xlsx','A2:A47')/1000;
f = xlsread('Lab 4 Data.xlsx','B2:B47')/1000;
n = length(x);
g_1 = 1;
g_2 = 1./(1+exp(-1*x));
g_1 = ones(size(x));
G=[g_1,g_2];
transG=transpose(G);
RHS=transG*f;
LHS=transG*G;
c=linsolve(LHS,RHS);
g_x=c(1)+c(2)*(1./(1+exp(-1*x)));
r=g_x-f;
R=sum(r.^2);
figure
plot(x,g_x)
hold on
plot(x,f,"*")
legend('g(x)','f data')
title('Lab 4 sigmoid Fit')
ylim([.0024 .0036])
xlim([-.2 3.5])
ft=@(x)(exp(x)-exp(-x))./(exp(x)+exp(-x));%here is the equation for hyperbolic tangent function
hold off;
plot(x,ft(x));
  2 Comments
Dawson Duran
Dawson Duran on 21 Oct 2022
I try to implement the hyperbolic tangent function but it keeps failing to yeild the same results. Could you show your code for obtaining the second graph? I have tried swapping formulas and editing to apply it but it doesn't seem to work.
David Hill
David Hill on 21 Oct 2022
x = xlsread('Lab 4 Data.xlsx','A2:A47')/1000;
ft=@(x)(exp(x)-exp(-x))./(exp(x)+exp(-x));
plot(x,ft(x));

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!