Info

This question is closed. Reopen it to edit or answer.

Why is there a difference in the error when using "syms approach" and "@(x) approach" while defining functions in MATLAB?

1 view (last 30 days)
I tried to plot the deviation of the solution of a first derivative of an arbitary function, say, f(x) = sin(x) at some arbitrary point x0 = 1.2, between forward difference scheme and exact solution.
I tried using two approaches:
1) Directly defining the functions as f = @(x) sin(x)
2) Defining x using syms anf then writing f = sin(x)
For various values of the h (in forward difference), I plotted the error in loglog plot as below using the following code.
%% Digits problem
clc;clear;
close all;
%%
power = -16:1:-1;
h = 10.^power;
%% Method 1
syms x
f = sin(x);
x0 = 1.2;
df = diff(f);
ddf = diff(df);
FD_df = (subs(f,x,x0 + h) - subs(f,x,x0))./h;
abs_error_f = abs(FD_df - subs(df,x,x0));
%% Method 2
g = @(x) sin(x);
x0 = 1.2;
dg = @(x) cos(x);
ddg = @(x) -sin(x);
FD_dg = (g(x0 + h) - g(x0))./h;
abs_error_g = abs(FD_dg - dg(x0));
%% Plotting
figure('Units','normalized','OuterPosition',[0 0 1 1])
loglog(h,abs_error_f,'o--',h,abs_error_g,'*--');
set(gca,'fontsize',15)
xlabel('h','Interpreter','latex')
ylabel('Absolute error','Interpreter','latex')
  3 Comments
Vigneshwaran Sankar
Vigneshwaran Sankar on 14 Sep 2020
Rik, thanks for your response. But can you elaborate on that. What is really going on behind the scene. Is one of the method is truncating the digits while performing calculation when compared to other?
Rik
Rik on 14 Sep 2020
I am not familiar with the inner workings of the symbolic toolbox, and it is possible that nobody is allowed to tell you without an NDA in place. They must both round the values to some extent, since computer memory has a limit. Although there is a quad data type, usually all values in Matlab will be stored in the double data type: a double precision IEEE floating point value. I don't know at what point the symbolic toolbox will round the value of pi or any of the numbers involved in your calculation, and I don't know if that even matters.
The premise of your question is that the two methods are the same, while they are not. Compare the two calculations below:
a=4;b=100
%option1:
c=a*b;
%option2:
a=uint8(a);b=uint8(b);
c=a*b;
c=double(c);
Both use the same mathematical idea and return the same data type: multiplying two numbers and returning the result as a double. But they will not return the same value, because during one of the steps, the second used a data type that didn't protect against an overflow.
I don't know what exactly happens under the hood for either method, but your premise that they are the same is flawed, and hence the expectation that they should return the same value is flawed as well.

Answers (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 14 Sep 2020
Method 2 is an approximate method for finding the derivative of a function

Community Treasure Hunt

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

Start Hunting!