I have been searching a function to do “subjecting” in Matlab. I'm not sure whether it is called as subjecting or by some other name. Let me explain the functionality.
Let's say I have an expression like this.
syms x, y,z;
y = 2*x^2 - 2*z + 1;
I want a function to get the x to one side and other variables to the other side.
ie.
x = ((y + 2*z - 1)/ 2 )^0.5
Is there any functions or built in Matlab command to do this?
Have you looked at the documentation of the MATLAB math toolbox at all? It's right there. The function is "solve". Read the documentation here: solve documentation.
Related
Sorry for the complicated headline.
my basics idea is:
I have a function, lets say z(t) = x(phi(t)) + y(phi(t))
The twist where I couldn't find anything is:
suppose I want to calculate the symbolic derivation dz(t)/dt BUT without knowing neither x(t) nor y(t) specifically.
Can Matlab deliver something aloong the line
dz/dt=dx/d phi * dphi/dt + dy/dphi * dphi/dt
And if so, how do I have to program that?
I wanted to know a specific thing about Matlab fsolve technique. As shown in the code, there are two non-linear equations. I can normally solve them but I want to optimise the process. It is observed that there is a common term 2*x(1)^2 - 3*x(2)^3 in both of these equations. The question is, can I declare a separate term for the common part and use the fsolve(as in the commented part)? If yes, can anyone please tell me the way to do.
function f = sym4(x)
f(1)= 8*x(1)^2 -12*x(1)^3 +x(1)*x(2)+ x(2)^2-1;
f(2)= 2*x(1)^3 - 3*x(1)^4 +x(2)-3;
% z = 2*x(1)^2 - 3*x(2)^3;
%
% f(1)= 4*z + x(2)^2 + x(1)*x(2) -1;
% f(2)= x*z +x(2)-3;
end
Yes, you can use intermediate variables, which may be somewhat faster because the common term is only evaluated once. As horchler pointed out, you should check your indexes.
I made an algorithm for solving an specific IVP. I get the solution with the function dsolve() in MatLab, but I don't want to get the solution in terms of the constants because I'm gonna replace the solution in my IVP.
For example, when I solve dsolve('Dy = x + y','x) ' I get C12*exp(x) - x - 1 but I only want to obtain exp(x) - x - 1. It's very straightforward to chop out the C12 by converting the sym to string, but I don't know if I try with a different function will it have more constants and only 'chopping' the first characters will work. So...
Is there a way to get the output of dsolve() without the constants?
You can simply add an initial condition and you'll have an output without constants.
Like dsolve('Dy = x + y','y(0)=0','x')
I have the following code:
syms t x;
e=symfun(x-t,[x,t]);
In the problem I want to solve x is a function of t but I only know its value at the given t,so I modeled it here as a variable.I want to differentiate e with respect to time without "losing" x,so that I can then substitute it with x'(t) which is known to me.
In another question of mine here,someone suggested that I write the following:
e=symfun(exp(t)-t,[t]);
and after the differentiation check if I can substitute exp(t) with the value of x'(t).
Is this possible?Is there any other neater way?
I'm really not sure I understand what you're asking (and I didn't understand your other question either), but here's an attempt.
Since, x is a function of time, let's make that explicit by making it what the help and documentation for symfun calls an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. In Matlab R2014b:
syms t x(t);
e = symfun(x-t,t)
which returns
e(t) =
x(t) - t
Taking the derivative of the symfun function e with respect to time:
edot = diff(e,t)
returns
edot(t) =
D(x)(t) - 1
the expression for edot(t) is a function of the derivative of x with respect to time:
xdot = diff(x,t)
which is the abstract symfun:
xdot(t) =
D(x)(t)
Now, I think you want to be able to substitute a specific value for xdot (xdot_given) into e(t) for t at t_given. You should be able to do this just using subs, e.g., something like this:
sums t_given xdot_given;
edot_t_given = subs(edot,{t,xdot},{t_given, xdot_given});
You may not need to substitute t if the only parts of edot that are a function of time are the xdot parts.
I'm new to Matlab and I'm attempting to use it to solve equations numerically. I consulted the Matlab documentation, found the following code:
numeric::solve(x^6 - PI*x^2 = sin(3), x)
I tried to execute it, but Matlab says:
numeric::solve(x^6 - PI*x^2 = sin(3), x)
|
Error: Unexpected MATLAB operator.
I'm confused. Could you tell me what's wrong? I'm using Matlab R2013a on OS X Mavericks.
try using
syms x
sol_x = solve(x^6 - pi*x^2 == sin(3), x);
sol_x = sym2poly(sol_x);
You try to use a command that only works in the MuPAD Notebook Interface in MATLAB. The documentation got better over time pointing this out. Does 2013a include the vpasolve command? If yes, that is probably what you are looking for.
Perhaps you typed "solve" and "matlab" into Google and came across this page. Note the warning in the yellow box at the top of the page that #ChristopherCreutzig alluded to in his answer. MuPAD is a separate environment available to Matlab users – type mupad in your command window and you'll be able to run your command – but its functions are not directly callable from within Matlab. Many (if not most) of the functions in the Symbolic Math toolbox use the MuPAD engine under the hood. In your case you can call numeric::solve from within Matlab like this:
syms x;
s = feval(symengine, 'numeric::solve', x^6 - sym(pi)*x^2 == sin(3), x)
or using the older string format:
s = feval(symengine, 'numeric::solve', 'x^6 - pi*x^2 = sin(3)', 'x')
The output of either can then be converted to double precision column vector with s = double(s.'). However, in this case there seems to be no reason not to use sym/solve instead:
syms x
s = double(solve(x^6 - sym(pi)*x^2 == sin(3), x).');
See this for further details and other options for calling MuPAD functions from within Matlab.