-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
%% Padé approximation of natural logarithms | ||
% This file contains the code to approximate the natural logarithm ($\ln x$) | ||
% using Padé approximation. The |pade| function provides approximation for many | ||
% functions. | ||
%% | ||
% *Preparation*: Clear everything and set formatting. | ||
|
||
clc | ||
format loose | ||
|
||
syms x | ||
f = log(x); % f is the natural logarithm of x | ||
%% | ||
% *Approximation*: Try to find the best approximant of the natural logarithm. | ||
% | ||
% _Note_: | ||
%% | ||
% * Drag the slider to adjust the center of the Padé approximant. | ||
% * 1 seems to produce a clear and simple output. | ||
|
||
a =1; | ||
|
||
% 1 is the best for now... | ||
% error: 0.0105 | ||
|
||
g = pade(f, x, a, 'Order', [5 5]) | ||
e = abs(f - g); | ||
%% | ||
% *Evaluation*: Find the errors of the approximation. | ||
|
||
x_values = linspace(0, 1, 1e2); | ||
err = double(subs(e, x, x_values)); | ||
mean(err, 'omitnan') | ||
%% | ||
% *Visualization*: Graph the errors and see the covergence. | ||
|
||
figure | ||
hold off | ||
hold on | ||
fplot(g, [-1 1]) | ||
fplot(f, [-1 1]) | ||
plot(err) | ||
|
||
title('Error of Padé approximants of ln(x)') | ||
legend('g (approximated)', 'f', 'error') | ||
xlabel('x') | ||
ylabel('error') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
%% Padé approximation of cosine | ||
% This file contains the code to approximate cosine using Padé approximation. | ||
% The |pade| function provides approximation for many functions. | ||
%% | ||
% *Preparation*: Clear everything and set formatting. | ||
|
||
clc | ||
format loose | ||
format short | ||
|
||
syms x | ||
f = cos(x); % f is the cosine value of x | ||
%% | ||
% *Approximation*: Try to find the best approximant of cosine. | ||
% | ||
% _Note_: | ||
%% | ||
% * Drag the slider to adjust the center of the Padé approximant. | ||
% * 0 seems to produce a clear and simple output. | ||
|
||
a =0; | ||
|
||
% 1 is the best for now... | ||
% error: 0.0105 | ||
|
||
g = pade(f, x, a, 'Order', [6 5]) | ||
e = abs(f - g); | ||
%% | ||
% *Evaluation*: Find the errors of the approximation. | ||
|
||
x_values = linspace(0, 1, 1e2); | ||
err = double(subs(e, x, x_values)); | ||
mean(err, 'omitnan') | ||
%% | ||
% *Visualization*: Graph the errors and see the covergence. | ||
|
||
figure | ||
hold off | ||
hold on | ||
fplot(g, [-1 1]) | ||
fplot(f, [-1 1]) | ||
plot(err) | ||
|
||
title('Error of Padé approximants of ln(x)') | ||
legend('g (approximated)', 'f', 'error') | ||
xlabel('x') | ||
ylabel('error') |