I am trying to do this: syms f(x, y) but it is not working and throws the error: Not a valid variable name. What am I doing wrong here? Thank you
Like this
syms x y;
f = sym('3*x + 4*y');
subs(f, {x,y}, {4,5}); % returns 32
Btw, quite a nice page on symbolic maths in MATLAB here
It looks valid since it is the same as an example in http://www.mathworks.co.uk/help/toolbox/symbolic/syms.html. You could check which x, which y and which f to make sure that x, y and f aren't existing functions (I don't think symbolic variables can have the same name as a function)
You could also try:
x = sym('x');
y = sym('y');
f(x, y) = sym('f(x, y)');
This should do the same as your original code, but I don't see why it would work if syms doesn't.
Related
I am trying to make a 3d plot but i am getting an error and im not sure how to solve it. I know that there are other questions out there similar to mine but i tried some of them and it did not work.
fh = sin(x)*cos(y).^3 + 2*cos(x).^5*sin(y)
[X,Y] = meshgrid(1:0.5:10,1:20);
surf(X,Y,fh)
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
The Z data in this case is what you are passing to surf as fh. It looks like fh is the function you want to use to compute Z, but you need to use the gridded values you generated for X and Y to evaluate it. As your code is now, it is evaluating the function using x and y (case matters!), which you haven't defined for us. Try this instead:
[X, Y] = meshgrid(1:0.5:10, 1:20);
Z = sin(X).*cos(Y).^3 + 2.*cos(X).^5.*sin(Y);
surf(X, Y, Z);
Notice that I used the .* operator (element-wise multiplication) instead of the * operator (matrix multiplication) in the equation.
You could also do this by defining an anonymous function that evaluates the formula for a given set of data:
fh = #(x, y) sin(x).*cos(y).^3 + 2.*cos(x).^5.*sin(y);
[X, Y] = meshgrid(1:0.5:10, 1:20);
surf(X, Y, fh(X, Y));
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
Is it possible to have MATLAB do implicit differentiation with its symbols?
I have the following code
syms x;
y = symfun(sym('y(x)'), sym('x'));
yPrime = symfun(sym('y+(2*x)-1'), [sym('x'), sym('y')]);
diff(yPrime, x, 1)
From this I get
ans(x, y) =
2
but what I want to get (in some form) is
ans(x, y) =
dy/dx + 2
Is that possible?
It looks you're trying to use y as both a symbolic variable and an abstract symbolic function (symfun), which isn't possible. A symbolic function can only take symbolic variables as arguments. I think that you can accomplish what you want with (old-style strings not needed)
syms y(x) % Implicitly defines x too
yPrime = y+2*x-1 % Also a symfun because y is a symfun
diff(yPrime,x)
which returns this symfun
ans(x) =
diff(y(x), x) + 2
I am trying to run the function mtaylor from the MuPAD engine in MatLab, which provides a multivariate Taylor expansion of a function. However, it keeps telling me I am trying to expand around an invalid point. Here is a minimal working example of what I tried:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
Error message:
vError using mupadengine/feval (line 157)
MuPAD error: Error: Invalid expansion point. [mtaylor]
Why doesn't this work?
The reason this works with MuPAD's mtaylor
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), x, 4) % [x] is fine too
and this doesn't
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
is that the [x, y] argument is seen as a single symbolic vector argument/variable rather than a list of variables for the expansion. Your expression, exp(x^2 - y), is not in terms of vector variables, but simple scalars, x and y.
The workaround is to pass the list as a string:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), '[x, y]', 4)
or
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), ['[' char(x) ',' char(y) ']'], 4)
or use evalin to write the MuPAD command a single string as suggested by #Daniel in the comments:
syms x y;
evalin(symengine,'mtaylor(exp(x^2 - y), [x, y], 4)')
Arrays and Matrices vs. Lists in MuPAD
For further clarification, arrays of symbolic variables in Matlab correspond to the MuPAD array type, which can be created via feval(symengine,'array','1..1','1..2','[x,y]'). More specifically, they are of type Dom::Matrix(), which can be created via V=feval(symengine,'Dom::Matrix()','[x,y]') or just syms x y; V=[x,y].
The mtaylor function requires a list input, which can be created via L=evalin(symengine,'[x,y]'). Thus
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), V, 4)
will produce the same error as in your question, but
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), L, 4)
will work correctly. Unfortunately, L and V appear identical from within Matlab, but you can use MuPAD's domtype function to differentiate them:
feval(symengine,'domtype',V)
feval(symengine,'domtype',L)
which returns Dom::Matrix() and DOM_LIST.
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