Best way to initialize a function to differentiate and plot it in Matlab -


i new matlab , looking advice on suitable or best way(s) initialize function ( = vs =@ or other options) able differentiate it, find value @ specific argument , plot tangent lines @ specific points.

things discovered:

f = function(x) % needs x initalized; returns values diff(f, x) plot(x, f)  f = @(x) function diff(f, x) % doesn't work fplot(f, x) 

and symbolically

syms x f = function 

but can't values, differentiate or plot f yet in last 2 cases.

given particular case, want provide function , find analytical derivative of it. once find this, wish determine derivative @ point is, plot tangent line coincident original function.

best way use syms define symbolic variable, differentiate diff did. now, what's left want use substitute in real values. can use crafted matlabfunction function you. take in symbolic mathematically defined equation , return function handle can accept numerical inputs. i'd add operators returned handle element-wise.

once have analytical derivative, can find slope of tangent line substituting point newly created function matlabfunction , you'd have use matlabfunction again convert original function function handle can find point on tangent line. you'd have point , slope , can find equation of tangent line via:

y -y0 = m*(x - x0) 

x0 desired point want, y0 output seen original function , m slope found using derivative.

the steps perform achieve want following:

  1. define symbolic variable x, t, whatever
  2. define original function
  3. compute derivative via diff
  4. convert both 2 , 3 function handles via matlabfunction
  5. define domain representative of function
  6. plot original function
  7. choose point want find slope of
  8. compute equation of tangent line , determine appropriate domain line
  9. plot line , emphasize point of interest

here's code @ each step... let's use y = x^3 , let's analyze slope @ x = 2 example:

%// step #1 syms t;  %// step #2 y = t^3;  %// step #3 dy = diff(y, t);  %// step #4 yh = matlabfunction(y); dyh = matlabfunction(dy);  %// step #5 x = -4:0.01:4;  %// step #6 figure; hold on; plot(x, yh(x));  %// step #7 x0 = 2; slope = dyh(x0);  %// step #8 %// y - y0 = m*(x-x0) %// y = m*(x - x0) + y0 tol = 1; x2 = x0-tol:0.01:x0+tol; y0 = yh(x0); out = slope*(x2 - x0) + y0;  %// step #9 plot(x0, y0, 'r.'); plot(x2, out, 'g'); 

let's go through each step slowly.

step #1

very straight forward. i'm using t variable here of interest.

step #2

i define function that's y = t^3 using symbolic variable defined earlier. again, easy.

step #3

we compute analytical derivative of function y defined in step #2 respect t.... easy.

step #4

we create numerical function handles both original function , derivative can substitute points... whether they're stored in arrays or matrices... can evaluate each value in array respect functions represent.

basically, need use matlabfunction can use these substitute whatever numbers want in.

step #5

i defined domain of -4 <= x <= 4 depend on function you're plotting. you'll have change depending on proper domain be. chose step size 0.01 because when plot things in matlab, plot array of points. allows me generate list of values -4 4 in steps of 0.01.

step #6

i spawn new figure, use hold on when call plot multiple times, append new figure. go ahead , plot original curve. take notice i'm using function handle created matlab original curve stored in yh.

step #7

i choose point of interest want, x0 = 2, determine output value @ point... stored in y0.

step #8

once compute desired point, compute slope seen @ point derivative function, re-arrange equation of tangent line produce output values given original domain created, (x0,y0) created previously. output of output of tangent line points. take note i'm centering line around point of interest (x0,y0) , define variable called tol plot line +/- tol centered around (x0,y0). specifically, plot line between x0 - tol , x0 + tol. set tol = 1 here, change according suitable you.

step #9

plot point of interest in red, , equation of tangent line in green. keep in mind number of points of tangent line don't match number of points original domain, make sure use right arrays right points.


here's get:

enter image description here


Comments