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);
Related
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.
I was going through matlab docs and it's mentioned that a polynomial can be created and evaluated as mentioned in this doc
However, I would like to create a third degree polynomial in x and y. Example: f(x, y) = x^3 + y^3 + 3x^2y + 3xy^2.
How do we create a third degree polynomial in x and y, like the example above?
The link you post is for creating a vector of coefficients for a polynomial of one variable. For two variables like you have, could you just use a function handle? E.g.,
f = #(x, y) x.^3 + y.^3 + 3*x.^2.*y + 3*x.*y.^2
I have an equation like this:
dy/dx = a(x)*y + b
where a(x) is a non-constant (a=1/x) and b is a vector (10000 rows).
How can I solve this equation?
Let me assume you would like to write a generic numerical solver for dy/dx = a(x)*y + b. Then you can pass the function a(x) as an argument to the right-hand side function of one of the ODE solvers. e.g.
a = #(x) 1/x;
xdomain = [1 10];
b = rand(10000,1);
y0 = ones(10000,1);
[x,y] = ode45(#(x,y,a,b)a(x)*y + b,xdomain,y0,[],a,b);
plot(x,y)
Here, I've specified the domain of x as xdomain, and the value of y at the bottom limit of x as y0.
From my comments, you can solve this without MATLAB. Assuming non-zero x, you can use an integrating factor to get a 10000-by-1 solution y(x)
y_i(x) = b_i*x*ln(x) + c_i*x
with 10000-by-1 vector of constants c, where y_i(x), b_i and c_i are the i-th entries of y(x), b and c respectively. The constant vector c can be determined at some point x0 as
c_i = y_i(x0)/x_0 - b_i*ln(x0)
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.