integral and derivatives of a function
15 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
2 Comments
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
More Answers (1)
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
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!