integral and derivatives of a function

조회 수: 8 (최근 30일)
Chloe St John
Chloe St John 2019년 1월 26일
편집: madhan ravi 2019년 1월 26일
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
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
Chloe St John
Chloe St John 2019년 1월 26일
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
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
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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by