Why is fplot in Matlab mathematically incorrect?

45 Views Asked by At

I am trying to get the derivative and integral of cos(x/8)*sin(x). Mathematically I know that f(x) should start at 0 and f'(x) should start at 1. However, my plot in Matlab isn't showing that.

I've tried rearranging the equations and setting upper and lower limits.
code

clc;clear;
syms x f(x) %declaring my symbolic variable
f(x)=cos(x/8)*sin(x);
dfdx=diff(f(x));
ind_ing=int(f(x));
fplot(f(x));
hold on;
fplot(dfdx);
fplot(ind_ing);
hold off
1

There are 1 best solutions below

0
Safdar Mirza On

The integral calculation int(f(x)) and the plotting of ind_ing might not be what you expect. The integral of the function should be a function of x, not a single value.

clc;
clear;

syms x;

f = cos(x/8) * sin(x);
dfdx = diff(f);

% Calculate the indefinite integral
F = int(f);

% Plot the function, its derivative, and the indefinite integral
fplot(f, [-20, 20]);
hold on;
fplot(dfdx, [-20, 20]);
fplot(F, [-20, 20]);
hold off;

legend('f(x)', "f'(x)", "Indefinite Integral");
title('Plot of f(x), f''(x), and the Indefinite Integral');

We calculate the indefinite integral F and then plot it over the same range as the original function and its derivative. This should give you a plot that shows the expected behavior, where the integral starts at 0 and the derivative starts at 1.