How to differentiate vectors

Hello, please I have a code with lambda and n given below. Please how do I obtain d(n)/d(lambda) and d^2(n)/d(lambda)^2 i.e. the first and second deriviative of n wrt lambda?
lambda = linspace(0.5,2.5);
n = [1.55155531233953 1.54949576778463 1.54767969992980 1.54606941077293 1.54463432037936 1.54334939304258 1.54219395936135 1.54115082328366 1.54020557725122 1.53934607132757 1.53856199764777 1.53784456219577 1.53718622338909 1.53658048225698 1.53602171281363 1.53550502400216 1.53502614662547 1.53458134019413 1.53416731575664 1.53378117163556 1.53342033964695 1.53308253988297 1.53276574252631 1.53246813546774 1.53218809673566 1.53192417093373 1.53167504903131 1.53143955097005 1.53121661064518 1.53100526289637 1.53080463220578 1.53061392285076 1.53043241030050 1.53025943367952 1.53009438914900 1.52993672407995 1.52978593191141 1.52964154760293 1.52950314360382 1.52937032627300 1.52924273269261 1.52912002782641 1.52900190198108 1.52888806853379 1.52877826189461 1.52867223567629 1.52856976104774 1.52847062525013 1.52837463025771 1.52828159156731 1.52819133710247 1.52810370622009 1.52801854880867 1.52793572446860 1.52785510176605 1.52777655755305 1.52769997634709 1.52762524976427 1.52755227600092 1.52748095935889 1.52741120981038 1.52734294259859 1.52727607787089 1.52721054034140 1.52714625898047 1.52708316672849 1.52702120023196 1.52696029959983 1.52690040817834 1.52684147234282 1.52678344130489 1.52672626693392 1.52666990359143 1.52661430797744 1.52655943898778 1.52650525758144 1.52645172665720 1.52639881093877 1.52634647686784 1.52629469250434 1.52624342743341 1.52619265267854 1.52614234062043 1.52609246492120 1.52604300045342 1.52599392323372 1.52594521036064 1.52589683995631 1.52584879111185 1.52580104383610 1.52575357900747 1.52570637832878 1.52565942428470 1.52561270010188 1.52556618971130 1.52551987771288 1.52547374934221 1.52542779043906 1.52538198741785 1.52533632723963];
plot(n,lambda)
ylabel('n','FontWeight','bold','FontSize',14)
xlabel('lambda','FontWeight','bold','FontSize',14)

 Accepted Answer

Use the gradient function —
lambda = linspace(0.5,2.5);
n = [1.55155531233953 1.54949576778463 1.54767969992980 1.54606941077293 1.54463432037936 1.54334939304258 1.54219395936135 1.54115082328366 1.54020557725122 1.53934607132757 1.53856199764777 1.53784456219577 1.53718622338909 1.53658048225698 1.53602171281363 1.53550502400216 1.53502614662547 1.53458134019413 1.53416731575664 1.53378117163556 1.53342033964695 1.53308253988297 1.53276574252631 1.53246813546774 1.53218809673566 1.53192417093373 1.53167504903131 1.53143955097005 1.53121661064518 1.53100526289637 1.53080463220578 1.53061392285076 1.53043241030050 1.53025943367952 1.53009438914900 1.52993672407995 1.52978593191141 1.52964154760293 1.52950314360382 1.52937032627300 1.52924273269261 1.52912002782641 1.52900190198108 1.52888806853379 1.52877826189461 1.52867223567629 1.52856976104774 1.52847062525013 1.52837463025771 1.52828159156731 1.52819133710247 1.52810370622009 1.52801854880867 1.52793572446860 1.52785510176605 1.52777655755305 1.52769997634709 1.52762524976427 1.52755227600092 1.52748095935889 1.52741120981038 1.52734294259859 1.52727607787089 1.52721054034140 1.52714625898047 1.52708316672849 1.52702120023196 1.52696029959983 1.52690040817834 1.52684147234282 1.52678344130489 1.52672626693392 1.52666990359143 1.52661430797744 1.52655943898778 1.52650525758144 1.52645172665720 1.52639881093877 1.52634647686784 1.52629469250434 1.52624342743341 1.52619265267854 1.52614234062043 1.52609246492120 1.52604300045342 1.52599392323372 1.52594521036064 1.52589683995631 1.52584879111185 1.52580104383610 1.52575357900747 1.52570637832878 1.52565942428470 1.52561270010188 1.52556618971130 1.52551987771288 1.52547374934221 1.52542779043906 1.52538198741785 1.52533632723963];
plot(n,lambda)
ylabel('\lambda','FontWeight','bold','FontSize',14)
xlabel('n','FontWeight','bold','FontSize',14)
dndlambda = gradient(n) ./ gradient(lambda); % First Numerical Derivative
d2ndlambda2 = gradient(dndlambda) ./ gradient(lambda); % Second NMumerical Derivative
figure
yyaxis left
plot(lambda, n, 'DisplayName','Original Data')
yyaxis right
plot(lambda, dndlambda, 'DisplayName','First Derivative')
hold on
plot(lambda, d2ndlambda2, 'DisplayName','Second Derivative')
hold off
grid
xlabel('\lambda','FontWeight','bold','FontSize',14)
legend('Location','best')
Note that the first asrgument to plot is the independent variable and the second argument is the dependent variable. I corrected the axis labels in the firsst plot to reflect this.
I used yyaxis because the magnitudes between the original data and the derivatives are significantly different.
.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!