equation manipulation in MATLAB - matlab

I have a problem using MATLAB for a dynamics problem. The problem is how to code in MATLAB, not how to solve the equation. I'm fairly new to MATLAB.
I have 2 equations of motion like
DDx_c =
((kx_c - (m_p(2Dtheta^2lsin(theta) - (2lcos(theta)((m_psin(2theta)Dtheta^2l^2)/2 + (m_psin(2theta)l^2)/2 - gm_psin(theta)l + F - b_cx_c))/(I + l^2m_pcos(theta)^2)))/2)(I + l^2m_pcos(theta)^2))/(l^2m_p^2cos(theta)^2)
and
DDtheta =
-((m_c + m_p)(F - b_cx_c + (l^2m_psin(2theta))/2 + (Dtheta^2l^2m_psin(2theta))/2 - glm_psin(theta) + (lm_pcos(theta)(- lm_psin(theta)Dtheta^2 + kx_c))/(m_c + m_p)))/(l^2m_p^2*cos(theta)^2)
My problem is that i would like to have these equations on State Space form, in order to generate a transfer function. My problem is, that i'm not able to separate the expressions in the equation. It could be done by hand ofcause, but i would realy like this program to be generic
Could a solution be to convert the equation to a string(char array), and copy the expressions separated by '+' and '-' to some new variables, like it could be done in C++. I'm just not sure how to do it.

Related

Matlab: duplicate results with symbolic solver for system of two trigonometric equations?

For a robotics test on Coursera I need to implement an easy version of inverse kinematics for a two-arm system.
This as a background info.
I put up the equations in symbolic form that already worked for the forward kinematics. Then I try to solve it and get two identical solution.
function [rads1,rads2] = computeRrInverseKinematics(X,Y)
syms theta1 theta2 ;
eqns= [cos(theta1)+cos(theta2+theta1)==X, sin(theta1)+sin(theta2+theta1)==Y];
[theta1, theta2]=solve(eqns, [theta1 theta2]);
rads1=theta1;
rads2=theta1;
What I should get or at least I thought is one value for theta1 and theta2 - two elation, two results, normally.
This is what I get:
t1 =
2*atan(7^(1/2)/3 + 4/3)
-2*atan(7^(1/2)/3 - 4/3)
t2 =
2*atan(7^(1/2)/3 + 4/3)
-2*atan(7^(1/2)/3 - 4/3)
Any idea what I did wrong?
Thanks.

Matlab - derivation of function of variable, but no function defined

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?

Get solution of an lineal ordinary differential equation without constants

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

Matlab - Symbolically Solving Inequalities

So, I'm trying to establish some new stability criteria for my simulations, and this involves a lot of convoluted inequalities. I've worked through the math a few times by hand, and it's very laborous; so, I wanted to figure out a way to automate the process (as I'm trying to find the best integration scheme from a stability perspective). Is there anyway to solve inequalities symbolically in Matlab? Here's what I'm trying to solve. In the following expression, x refers to the gradient of a force function with respect to x, and t is the time step. In general, x < 0 and t > 0:
-(t*x + (2*t^3*x + t^2*x^2 - 2*t*x + 4*t + 1)^(1/2) + 1)/(x*t^2 - 2) < 1
Based on what I've looked at online, this seems to be possible in MuPAD, but using the following code does not give me any valid results:
solve(-(t*x + (2*t^3*x + t^2*x^2 - 2*t*x + 4*t + 1)^(1/2) + 1)/(x*t^2 - 2) < 1, t)
Any idea what I can do to make this work and automate the process?
First, since Wolfram Alpha gives you an answer (that I presume you've checked for correctness), I assume you want to use Matlab to solve other similar problems. However, this is a very non-trivial inequality due to the roots of the polynomials. I haven't been able to get Matlab/MuPAD to do anything with it. As I stated, regular Matlab can solve inequalities and systems of equalities in many cases, e.g., in R2013b
syms x real;
solve(x^3-1>1,x)
Even Mathematica 9 has trouble (the Reduce function can be used instead of it's Solve, but the output is not easy to use).
You can, however, solve for the real roots where x < 0 and t > 0 via
syms x t real;
assume(x<0);
assume(t>0);
f = -(t*x+sqrt(2*t^3*x+t^2*x^2-2*t*x+4*t+1)+1)/(x*t^2-2);
s = solve(f==1,t)
which returns:
s =
-(x + (x*(x - 2))^(1/2))/x
This simplifies to sqrt((x-2)/x)-1. Thus t > sqrt((x-2)/x)-1, one of the bounds provided by Wolfram Alpha. (The other more complicated bound is always less than zero and actually is the condition that ensures that t is real.)
But, do you even need to be solving this problem symbolically? Do you need explicit expressions for the various intervals in terms of all x? If not, this type of problem is probably better suited numeric approaches – either via root solving (e.g., fzero) or minimization (e.g., fmincon).

Matlab function to isolate a variable in an equation

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.