Functions in matlab - matlab

This looks really simple. I want to define a function:
syms x
f = x^2
I want to be able to do f(4) and it spits out 16. I also want to avoid having to write a new m-file.

When dealing with symbolic variables, to substitute in a numeric value, use subs(), i.e. symbolic substitution:
syms x
f = x^2
subs(f,4)

>> f = #(x) x^2;
>> f(4)
ans =
16

Related

Evaluate symbolic matrix in MATLAB

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?
Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.
We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

Computing integral with variable bounds in MATLAB

Consider the following MWE in MATLAB:
f = #(t) integral(#(x) x.^2,0,t);
integral(f,0,1);
This yields the error
Error using integral (line 85) A and B must be floating-point scalars.
(and a bit more). How do I fix this? Is this even possible? I think the problem is the variable upper bound.
If you want to use integral then set 'ArrayValued' to true otherwise t would be an invalid end point in integral(#(x) x.^2,0,t). So it would be:
f = #(t) integral(#(x) x.^2,0,t);
integral(f,0,1,'ArrayValued',true)
% ans =
% 0.0833
Alternately, since you're doing double integration, so use the function dedicated for this purpose i.e. integral2. For your example, it would be:
f = #(t,x) x.^2 ;
integral2(f,0,1,0, #(t) t)
% ans =
% 0.0833
If you have Symbolic Math Toolbox, you can also use int as int(expr,var,a,b) but it would be slower. For your case, it would be:
syms x t;
f = x.^2;
req = int(int(f,x,0,t),t,0,1); % It gives 1/12
req = double(req); % Convert to double if required

differentiate a function in matlab

how can I differentiate a function in matlab? I tried with:
syms x;
f = 3x^2 + 2;
A = diff(f);
disp(A);
My problem is now I want to give x a value after (for example A(x = 1) and I don't know how.
First, there's an error in your code, you can't imply multiplication, you need to write 3*x^2 rather than 3x^2.
Also, in case you feed the function a vector rather than scalar, you should use element-wise operations (including a . before powers, multiplication etc.) so this becomes 3*x.^2.
Other than that, 2 options:
syms x;
f = 3*x.^2 + 2;
1) define A like you did and use subs to substitute x for, say, 1.
A = diff(f);
subs(A,x,1);
2) define A as an anonymous function and call it more easily.
A = #(y) subs(diff(f),x,y)
Now A can be easily called with different values
A(1)
ans = 6
A(2)
ans = 12
Links to documentation on element-wise ops, subs and anonymous functions:
https://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
https://uk.mathworks.com/help/symbolic/subs.html
https://uk.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
Modify your code like this:
syms x;
f = 3*x^2 + 2; % You missed a * here
A(x) = diff(f); % This creates a symfun
A(1) % This gives the value of A at x=1
You need the matlabfunction function.
matlabFunction Generate a MATLAB file or anonymous function from a sym

Matlab operation on anonymous functions

I have two anonymous functions f and g and I need to compute the integral over f*g using the quad function.
I tried the following which didnt not work:
h=#(x)(f(x)*g(x))
quad(h,-1,1);
Is there any way to make this work ?
Short answer
It depends on how f and g are defined, but you probably just need to add a dot:
h = #(x) f(x).*g(x);
Long answer
According to quad's documentation,
Q = quad(FUN,A,B)
[...] The function Y=FUN(X) should accept a vector argument X and return a vector result Y, the integrand evaluated at each element of X.
Assuming f and g already satisfy this requirement, h should be defined with .* (element-wise multiplication) so that it will fulfill it too.
Example:
>> f = #(x) x;
>> g = #(x) x.^2;
>> h = #(x) f(x).*g(x);
>> quad(h, 0, 1)
ans =
0.250000000000000

how do you do symbolic differentiation on function handle?

Say if i define the following:
g = #(x) x/sqrt(x^2+1)
How do i get the derivative function for g, which i can then use to evaluate at different points?
I tried the symbolic math toolkit, and tried the following:
>> syms x
>> f = x/sqrt(x^2+1)
f =
x/(x^2 + 1)^(1/2)
>> diff(f)
ans =
1/(x^2 + 1)^(1/2) - x^2/(x^2 + 1)^(3/2)
However, i cannot figure out how to turn this into a function handle/evaluate at different points. However, i prefer doing differentiation on function_handle.
Thank you very much!
Jason
You can use matlabFunction to convert a symbolic equation to a function. For example:
syms x1 x2;
f1 = x1^2+x2^2;
Df1 = jacobian(f1, [x1 x2]);
Df1 = matlabFunction(Df1);
Then Df1(0, 0) returns [0 0] as expected.
The function matlabFunction was introduced in version 5.2 (R2009a) of the Symbolic Math Toolbox.