Differentiation with respect to a self-defined function in matlab - matlab

I have made a function in func.m which is defined as follows:
function f = func(b)
f = b^2+1;
end
now i wish to differentiate with respect to that function i.e.
find 2*b+1 with a given value for b (lets say 1)
I tried diff(func(1)) but it returned an empty matrix.
Any ideas?
thanks

Your input argument func(1) is a vector of size 1x1. The function diff caled on vectors is this one: here. If you want to use the symbolic one, which allows to differentiate a function but requires the symbolic toolbox, you have to use a symbolic input. Explained here
Example:
% create a symbolic variable x
syms x
%differentiate f(x) and name it f1(x)
f1(x)=diff(f(x))
%get the value at point 1
f1(1)
If you don't have the symbolic toolbox, there is also a numeric solution.

Related

Matlab set range of inputs and step

I have this signal:
x(t) = t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
and I want to calculate the values only from -5pi to 5pi with a step of pi/100 using Matlab. How could I do it?
Provided you have defined m and k somewhere, and you have the matlab symbolic toolbox which provides the heaviside function, this is how it is done:
% First we define the function
x = #(t) t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
% Then we define the values for which we want to compute the function
t_values = -5*pi:pi/100:5*pi;
% Finally we evaluate the function
x_values = x(t_values)
Details
First line we define your function as an anonymous function which is a handy tool in matlab.
Then we create a vector of values from -5pi to 5*pi with steps of pi/100. For this we use the matlab colon syntax. It makes it short and efficient.
Finally we evaluate the function on each of the t_values by passing the vector to the anonymous function.
Note: If you don't have the symbolic toolbox, you could easily implement heaviside yourself.

How can I derive symbolic function in MATLAB in terms of another symbolic function?

gr = 9.81; %gravity
syms phi(t) m l
theta=1/3*m*l^2;
phidot=diff(phi,t);
U=m*gr*l/2*cos(phi);
T=1/2*theta*phidot^2+(1/2*phidot*l)^2*m;
L=T-U;
frst=diff(L,phidot);
The code is shown above. As you can see that phi(t) is symbolic time dependent function and phidot is derivation of it(also time dependent). L is obtained using these symbolic functions.
So, The problem is I can't derive L in terms of phidot in Matlab. The error occurs as following:
Error using sym/diff (line 26)
All arguments, except for the first one, must not be **symbolic** functions.
Error in pndlm (line 11)
frst=diff(L,phidot)
Is there any way to derive symbolic function in terms of another symbolic function? If not, Can you suggest me another alternative for avoiding this kind of error?
Possible duplicate of this
If you want to differentiate L with respect to q, q must be a
variable. You can use subs to replace it with a function and calculate
d/dt(dL/dq') later.
Remember those derivatives are partial. Hence just include explicitly the variable and obtain the expression.
% Variables
syms q qt m l g
theta=1/3*m*l^2;
% Lagrangian
U=m*g*l/2*cos(q);
T=1/2*theta*qt^2+(1/2*qt*l)^2*m;
L=T-U;
% Partial Derivatives
dLdq=diff(L,q)
dLdqt=diff(L,qt)
syms qf(t)
% Time Derivatives
qtf(t)=diff(qf,t)
dLdqf=subs(dLdqt,qt,qtf)
% Solution
m=1;l=1;g=9.81;
dsolve(diff(dLdqf,t)-dLdqf==0)

Matlab's subs does not evaluate symbolic Bessel function

I experienced a strange behaviour in Matlab with the function subs and the built-in besselj:
syms x
f = besselj(1,x)
subs(f,1)
returns
besselj(1, 1)
even though the documentation states
"subs(s,new) returns a copy of s replacing all occurrences of the default variable in s with new, and then evaluating s.
The default variable is defined by symvar.
So I would expect the output of subs(f,1) to be 0.4401. eval(subs(f,1)) produces the correct output of 0.4401. Does anyone know why this is the case?
I feel like you're trying to define an anonymous function, you don't need subs or eval here, simply use f as an actual function...
% No need to use symbolic math toolbox for x or f
f = #(x) besselj(1,x); % Define f as an anonymous function of x
f(1) % Evaulate f at a given point, say x = 1
>> ans = 0.4401
Side note: If for some reason you're really set on using symbolic variables (they seem overkill here) then you may just want to use the eval function and a symbolic function handle instead of subs.
syms f(x) % Defines f as a symfun and x as a sym variable
f(x) = besselj(1,x); % Define the function
eval(f(1)) % Evaluate at x=1
>> ans = 0.4401
In answer to your question about why subs doesn't "evaluate" the answer when using subs(f, 1)... It's probably because the nature of the besselj function. Because you're using symvars, you are using the symbolic math package's besselj function (as opposed to the core-package function by the same name).
This means that a symbolic expression is displayed to the command window when subs is used, in the same way that any other symbolic expression is displayed without needing to be evaluated - i.e. they can be simplified but don't run other functions.
This is why you then need to run eval, to evaluate the simplified symbolic expression.

MATLAB finding roots, and # operator

x=1;
f=#(x) x^3 - (5/x^2)-4*sin(x)-2;
fzero(f,x)
ans =
1.9227
I am supposed to find the root of the equation, x^3 - (5/x^2)-4*sin(x)-2, and the above code is the solution for it.
I don't understand the general mechanism of this code.
(1) What does # operator do?
I know its something like function handle, but I don't understand what function handle is.
(2) How does it work when it includes x in the parenthesis?
(3) How can there be a function fzero(), when I haven't made a script for fzero()?
(4) why are there two variables inside fzero()?
I don't understand that the variable 'f' does there
(5) Why did it declare x=1 in the beginning?
Please consider that I am pretty much new to MATLAB, and don't know much.
f = #(x) ... is the way to declare an anonymous function in MATLAB, actually not very different than creating a function normally in MATLAB such as function output = f(input) .... It is just the pratical way especially when you are working with mathematical functions.
#(x) defines that x is the variable of which is the same as f(x) in mathematics. fzero() is MATLAB's existing function to calculate the x value for f(x) = 0 which means calculating roots of defined funtion. Giving your x a real value at the beginning does mean the starting point to find the root. It will find the roots greater than 1 in your case. It will be very clear for you when you read existing documentation of MATLAB.
Edit:
If you give an interval such as x = [0 1] instead of x = 1, fzero(f,x) would try to calculate roots of f function in given interval, if there is no roots exist in that interval it woud return a NaN value.

How to go from a symbolic expression to a vector in MATLAB

I am working on a function that will generate polynomial interpolants for a given set of ordered pairs. You currently input the indexes of the node points in one vector, and the values of the function to be interpolated in a second vector. I then generate a symbolic expression for the Lagrange polynomial that interpolates that set of points. I would like to be able to go from this symbolic form to a vector form for comparison with test functions and such. That is, I have something that generates some polynomial P(x) in terms of some symbolic variable x. I would like to then sample this polynomial to a vector, and get values for the polynomial over (for example) linspace(-1,1,1000). If this is possible, how do I do it?
I guess I'll include the code that I have so far:
function l_poly = lpoly(x,f)
% Returns the polynomial interpolant as computed by lagrange's formula
syms a
n=size(x,2);
l_poly_vec = 1;
l_poly=0;
for k=1:n,
for l=1:n,
if (k ~= l)
l_poly_vec=l_poly_vec*(a-x(l))/(x(k)-x(l));
end
end
l_poly=l_poly+f(k)*l_poly_vec;
l_poly_vec = 1;
end
I plan on adding a third (or possibly fourth) input depending on how I can solve this issue. I'm guessing I would just need the length of the vector I want to sample to and the endpoints.
If I understand you correctly, you've constructed a Lagrange interpolating polynomial using the symbolic toolbox and now wish to evaluate it over a vector of values. One way to do this is to use the function sym2poly to extract the coefficients of the symbolic polynomial, and then use polyval to evaluate it. Alternatively, you could use matlabFunction to convert your symbolic expression into a regular Matlab function; or use subs to substitute in a numeric value for 'x'.
However, you would probably be better off avoiding the symbolic toolbox altogether and directly constructing the coefficients of the Lagrange interpolating polynomial, or, better yet, use a different interpolation scheme altogether. The function interp1 might be a good place to start.