mtaylor MuPAD - Matlab - matlab

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.

Related

How do I make this continuous regression?

Given the function f(x) = ln(x) with x belonging to [1,e] You must develop an algorithm in matlab that performs the following tasks.
Perform the continuous regression of f(x) onto the subspace 〈1, x, x^2〉, using the symbolic tools in Matlab. Plot the result (the function and the fitting).
Perform the discrete regression of f onto 〈1, x, x^2〉. For that purpose:
2.1. Introduce as regular sampler of the interval [1, e] using 1000 points. Call this sampler xs.
2.2. Evaluate f(x) on xs, and the basis functions 1, x, x^2 to generate {1, x, x^2, f}.
2.3. Perform the regression of f onto 〈1, x, x^2〉.
2.4. Plot the result (the function and the fitting)
i tried with this but it is not giving the right answer, and it is giving an error in plot(xs, f_values)
clear all
close all
clc
syms x
f = log(x);
V = [1 x x^2];
coeffs = V\f;
a0 = coeffs(1);
a1 = coeffs(2);
a2 = coeffs(3);
fitting = a0*1+ a1*x + a2*x^2;
ezplot(f,[1,exp(1)])
hold on
ezplot(fitting,[1,exp(1)])
legend('f(x)','fitting')
xs = linspace(1,exp(1),1000);
linspace(1,exp(1),1000)
f_values = subs(f, x, xs);
basis_1 = ones(1, 1000);
basis_x = xs;
basis_x2 = xs.^2;
V = [basis_1; basis_x; basis_x2];
fitting_values = coeffs(1)*basis_1 + coeffs(2)*basis_x + coeffs(3)*basis_x2;
plot(xs, f_values)
hold on
plot(xs, fitting_values)
legend('f(x)', 'fitting')
Looking solely at your error in plot(xs, f_values):
MATLAB's page for the function plot shows that when accepting two inputs (in your case, xs and f_values), they are either (x, y), or (y, line options). But since one of the values you've inputted are based on x (a symbolic value), it's a symbolic vector. plot can't handle symbolic inputs.
I believe the equivalent for syms is fplot from the symbolic maths toolbox.

Z must be a matrix, not a scalaqr or vector, matlab

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));

Implicit derivative with Matlab symbols

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

Rearranging symbolic expression

I have a function in MuPAD in terms of various variables as z = f(x,y,...).
Now I would like to rearrange the equation to express x in terms of z. I have not found the suitable command to do so.
Use solve. Example:
syms x y z
X = solve('z = x^3 + y^3 - 1', x);

Create symbolic function

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.