integral and derivatives of a function
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
madhan ravi
2019-1-26
编辑:madhan ravi
2019-1-26
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 个评论
madhan ravi
2019-1-26
编辑:madhan ravi
2019-1-26
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
更多回答(1 个)
madhan ravi
2019-1-26
编辑:madhan ravi
2019-1-26
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!