# integral and derivatives of a function

7 views (last 30 days)
Chloe St John on 26 Jan 2019
Edited: madhan ravi on 26 Jan 2019
trying to attain the integral and derivative of a function, and then plot the derivative
ya(x)=sin^3((2/(x+2))-3)
i saved the function as file a3
function [ya] = a3(x)
ya = (sin((2./(x+2))-3)).^3;
end
then on a seperate file my code is:
clc
clear
inta3=integral(@a3,0,5)
x=0:0.1:5
Ax=a3(x)
diffa3= diff(Ax)/x
figure
plot(x,Ax)
hold on
xlabel('X')
ylabel('Y')
title('Derivative of 3a')
in the command window it says this when i run the code:
Error using /
Matrix dimensions must agree.
Error in a4 (line 10)
diffa3= diff(Ax)/x
i think th integral part is fine its just the derivative part

madhan ravi on 26 Jan 2019
Edited: madhan ravi on 26 Jan 2019
clc
clear
inta3=integral(@a3,0,5)
syms x % requires symbolic toolbox
Ax=a3(x)
diffa3= diff(Ax)/x % remember diff(Ax) is one element lesser than x always!
figure
fplot(x,Ax,[0 5])
hold on
xlabel('X')
ylabel('Y')
title('Derivative of 3a')
function ya = a3(x)
ya = (sin((2./(x+2))-3)).^3;
end
Chloe St John on 26 Jan 2019
thank you , i also want to obtain the derivative in the range of 0<=x<=5 do you know how i do this ?
and i tried researching the syms x tool but still not sure why it is necessary
thank you for your help !
madhan ravi on 26 Jan 2019
Edited: madhan ravi on 26 Jan 2019
as you can see syms (wasn't necessary though) was used because it gives the analytical form of the derivative thereby to your latter question the derivative can be plotted in the rage from 0 to 5
fplot(...,[0 5])
% ^^^^^----- denotes 0<=x<=5

madhan ravi on 26 Jan 2019
Edited: madhan ravi on 26 Jan 2019
clc
clear
inta3=integral(@a3,0,5);
x=0:0.1:5
Ax=a3(x);
diffa3= diff(Ax)./0.01;
% missed it-----^ ^^^^----- should be the step size
figure
plot(x(1:numel(Ax)),Ax) % look here
hold on
xlabel('X')
ylabel('Y')
title('Derivative of 3a')
function ya = a3(x)
ya = (sin((2./(x+2))-3)).^3;
end