System linearization with Maple - does not evaluate to a module error - maple

I'm trying to apply the steps provided in this tutorial in order to linearize a system using Maple.
Create your system of non linear equations
Define your linearization points
Call the Linearize function and you are done.
I've tried with my system but it does not work. So I decided to try with the equations of the pendulum provided by the tutorial. Once the equations are written, I create the system with:
sys3 := [diff(x(t), t) = y(t), diff(theta(t), t) = phi(t), diff(y(t), t) = -(-3*cos(theta(t))*sin(theta(t))*g-2*u(t)+2*m*L*sin(theta(t))*phi(t)^2)/(-3*cos(theta(t))*m+2*M+2*m), diff(phi(t), t) = -(3*(-sin(theta(t))*g*M-sin(theta(t))*g*m-m*u(t)+m^2*L*sin(theta(t))*phi(t)^2))/((-3*cos(theta(t))*m+2*M+2*m)*m*L)]
set the list with the linearization points
lin_point3 := [phi(t) = 0, x(t) = 0, y(t) = 0, theta(t) = 0, u(t) = 0]
But when I call the Linearize function and press enter the enter the output is different from the one in the tutorial.
lin_model3 := Linearize(sys3, [u(t)], [phi(t), x(t), y(t), theta(t)], lin_point3)
When I press the PrintSystem(lin_model3) command I get a does not evaluate to a module error
How can I get this example working? Solution: Solved using Maple 18.

Related

Solving a simple system of differential equations

How would I numerically solve for the following simple system of differential equations using Octave?
Note:
I use the qualifier "simple" as, from my understanding, the system is
first order and is not coupled.
I have tried every
method and script online to try solve this including here,
here and here. In all options, I either get a hanging,
non-responsive Octave, a prompt stating "repeated convergence
failures", an error with recommendation that I manually adjust
the initial and maximum step size (which I did try and do, to no
avail), or something that initially seems like a solution on account of no errors but plotting the solution shows a blank graph
Where Octave provided for equivalent Matlab routines, I tried the various routines ode45, ode23, ode113, ode15s, ode23s, ode23t, ode23tb, ode15i and of course, Octaves own lsode command, all giving the same errors described above.
Let's first replicate the vanilla solution
% z = [x,y]
f = #(t,z) [ z(1).^2+t; z(1).*z(2)-2 ];
z0 = [ 2; 1];
[ T, Z ] = ode45(f, [0, 10], z0);
plot(T,Z); legend(["x";"y"]);
The integrator fails as reported with the warning
warning: Solving was not successful. The iterative integration loop exited at time t = 0.494898 before the endpoint at tend = 10.000000 was reached. This may happen if the stepsize becomes too small. Try to reduce the value of 'InitialStep' and/or 'MaxStep' with the command 'odeset'.
Repeating the integration up to shortly before the critical time
opt = odeset('MaxStep',0.01);
[ T, Z ] = ode45(f, [0, 0.49], z0, opt);
clf; plot(T,Z); legend(["x";"y"]);
results in the graph
where one can see that the quadratic term in the first equation leads to run-away growth. For some reason the solver does only recognize the ever reducing step size, but not the run-away values of the solution.
Indeed the first is a Riccati equation which are known to have poles at finite times. Using the typical parametrization x(t)=-u'(t)/u(t) has by the product/quotient rule the derivative
x' = -u''(t)/u(t) - u'(t)* (-u'(t)/u(t)^2) = -u''(t)/u(t) + x(t)^2
which then results in the ODE for u
u''(t)+t*u(t)=0, u(0)=-1, u'(0)=x(0)=2,
which is an Airy equation with the oscillating branch for t>0. The first root of u is a pole for x, there is no way to extend the solution beyond this point.
g=#(t,u) [u(2); -t.*u(1)]
u0 = [ 1; -2];
function [val,term, dir] = event(t,u)
val = u(1);
term = 0;
dir = 0;
end
opt = odeset('MaxStep',0.1, 'Events', #(t,u) event(t,u));
[T,U,Te,Ue,Ie] = ode45(g,[0,4],u0,opt);
disp(Te)
clf; plot(T,U); legend(["u";"u'"]);
which lists the zeros of u as 0.4949319379979706, 2.886092605590324, again confirming the reason for the warning, and gives the plot

User Made Matlab Function Fails Once All Variables are Cleared

I originally asked this question yesterday and found the answer myself; however, I used the clear all command in Matlab and now the function throws an error Undefined function or variable 'y'.
I used the code from my answer
function [s1] = L_Analytic3(eqn,t0,h,numstep,y0)
%Differential Equation solver for specific inputs
% eqn is the differential equation
% t0 is start of evaluation interval
% h is stepize
% numstep is the number of steps
% y0 is the initial condition
syms y(x)
cond = y(0) == y0;
A = dsolve(eqn, cond);
s1 = A;
S1 = s1;
for x = t0 : h : h*(numstep)
subs(x);
if x == t0
S1 = subs(s1,x);
else
S1 = [subs(S1), subs(s1,vpa(x))];
end
end
end
and also put L_Analytic3(diff(y) == y,0,0.1,5,1) into the Command Window after entering clear all. I have to run a seperate code
syms y(x)
cond = y(0) == 1;
A = dsolve(diff(y) == y, cond);
before using my function in order for the function to work. Is this just because A,ans,cond,x, and y, are already defined by the 3 line code before using the function? If so, is there a way that I can use the function without having to use that 3 line code first?
When you do L_Analytic3(diff(y) == ...); you do not have variable y defined, so MATLAB complains - it has no way of knowing y is a symbol that will be defined in the function you are calling. You do not require all 3 lines of code. syms y(x) should be enough to define y and lets you use the function call you wanted.
Now, there are 2 easy ways to fix this that I see:
A script (or another function) that has syms y(x), followed by the call to L_Analytic3 the way you are doing it (which now does not need syms y(x), it has been defined already).
Give anonymous equation as the input instead, say #(x) diff(x)==x, and change a line of L_Analytic3 slightly to A = dsolve(eqn(y), cond);
Both ways work fine for this, no idea if 2nd one breaks in more complex cases. I would likely pick 1st version if you are doing symbolic stuff, and 2nd if you would like to have same function call to both numeric and symbolic functions.

Simulating nonlinear differential quation in MATLAB?

I was assigned a program where I have to create a MATLAB model for the equation and figure as follows-- http://i.stack.imgur.com/wV0ro.png. Unfortunately, I've been stuck for quite a while.
or dh/dt = (-r^2*sqrt(2*g*h))/(0.5r+htan(phi))^2
where
g=386.4 in/s^2
h = 2+(34/64) in
r = 1/10 in
angle=30.519612098961595 degrees
I calculated for dh/dt, which is -0.185963075319895 in/s and time to empty t=13.611573134321043 s, which I did by t=h/-c1_solution.
My x and y range are:
t1_span = [0 t]
y1_span = [0 ; h]
My function is like this so far:
function hvt1 = leak(r,h,angle, g)
c1_solution_1 = (-(r(y1_span))^2 * sqrt(2*g*h(y1_span))) ;
c1_solution_2 = (0.5*r(t1_span)+h(t1_span)*tand(angle))^2 ;
c1_solution = c1_solution_1(1)/c1_solution_2(1) ;
hvt1 = c1_solution ;
So, this definitely needs work. I'm very inexperienced with this type of thing. I'm wondering how I can model how the container drains as a function of time. I'm guessing I'll have to use ode45. I know how to format ode45 in my program, but I'm having trouble with creating the functions.
Thanks in advance. :)
There is nothing mysterious or unclear about the documentation of ode45. As I said in comment, it's unclear what you are asking, and you should rewrite the differential equation in your post instead of linking to an external image...
In this example, you can replace solver by any matlab solver. r,g,phi are the constants defined by your problem, h0 is your initial state and tspan is the timespan in which you are considering your solution. Some solvers allow you to specify a timestep, others choose it dynamically.
[t,h] = solver( #dh_dt, tspan, h0 );
function dh = dh_dt(t,h)
dh = - r*r*sqrt(2*g*h);
dh = dh / (.5*r + h*tan(phi))^2;
end

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

I am trying to calculate the convolution of
x(t) = 1, -1<=t<=1
x(t) = 0, outside
with itself using the definition.
http://en.wikipedia.org/wiki/Convolution
I know how to do using the Matlab function conv, but I want to use the integral definition. My knowledge of Matlab and WolframAlpha is very limited.
I am still learning Mathematica myself, but here is what I came up with..
First we define the piecewise function (I am using the example from the Wikipedia page)
f[x_] := Piecewise[{{1, -0.5 <= x <= 0.5}}, 0]
Lets plot the function:
Plot[f[x], {x, -2, 2}, PlotStyle -> Thick, Exclusions -> None]
Then we write the function that defines the convolution of f with itself:
g[t_] = Integrate[f[x]*f[t - x], {x, -Infinity, Infinity}]
and the plot:
Plot[g[t], {t, -2, 2}, PlotStyle -> Thick]
EDIT
I tried to do the same in MATLAB/MuPad, I wasn't as successful:
f := x -> piecewise([x < -0.5 or x > 0.5, 0], [x >= -0.5 and x <= 0.5, 1])
plot(f, x = -2..2)
However when I try to compute the integral, it took almost a minute to return this:
g := t -> int(f(x)*f(t-x), x = -infinity..infinity)
the plot (also took too long)
plot(g, t = -2..2)
Note the same could have been done from inside MATLAB with the syntax:
evalin(symengine,'<MUPAD EXPRESSIONS HERE>')
Mathematica actually has a convolve function. The documentation on it has a number of different examples:
http://reference.wolfram.com/mathematica/ref/Convolve.html?q=Convolve&lang=en
I don't know much about Mathematica so I can only help you (partially) about the Matlab part.
To do the convolution with the Matlab conv functions means you do it numerically. What you mean with the integral definition is that you want to do it symbolically. For this you need the Matlab Symbolic Toolbox. This is essentially a Maple plugin for Matlab. So what you want to know is how it works in Maple.
You might find this link useful (wikibooks) for an introduction on the MuPad in Matlab.
I tried to implement your box function as a function in Matlab as
t = sym('t')
f = (t > -1) + (t < 1) - 1
However this does not work when t is ob symbolic type Function 'gt' is not implemented for MuPAD symbolic objects.
You could declare f it as a piecewise function see (matlab online help), this did not work in my Matlab though. The examples are all Maple syntax, so they would work in Maple straight away.
To circumvent this, I used
t = sym('t')
s = sym('s')
f = #(t) heaviside(t + 1) - heaviside(t - 1)
Unfortunately this is not successful
I = int(f(s)*f(t-s),s, 0, t)
gives
Warning: Explicit integral could not be found.
and does not provide an explicit result. You can still evaluate this expression, e.g.
>> subs(I, t, 1.5)
ans =
1/2
But Matlab/MuPad failed to give you and explicit expression in terms of t. This is not really unexpected as the function f is discontinuous. This is not so easy for symbolic computations.
Now I would go and help the computer, fortunately for the example that you asked the answer is very easy. The convolution in your example is simply the int_0^t BoxFunction(s) * BoxFunction(t - s) ds. The integrant BoxFunction(s) * BoxFunction(t - s) is again a box function, just not one that goes from [-1,1] but to a smaller interval (that depends on t). Then you only need to integrate the function f(x)=1 over this smaller interval.
Some of those steps you would have to to by hand first, then you could try to re-enter them to Matlab. You don't even need a computer algebra program at all to get to the answer.
Maybe Matematica or Maple could actually solve this problem, remember that the MuPad shipped with Matlab is only a stripped down version of Maple. Maybe the above might still help you, it should give you the idea how things work together. Try to put in a nicer function for f, e.g. a polynomial and you should get it working.

How do I make a function from a symbolic expression in MATLAB?

How can I make a function from a symbolic expression? For example, I have the following:
syms beta
n1,n2,m,aa= Constants
u = sqrt(n2-beta^2);
w = sqrt(beta^2-n1);
a = tan(u)/w+tanh(w)/u;
b = tanh(u)/w;
f = (a+b)*cos(aa*u+m*pi)+a-b*sin(aa*u+m*pi); %# The main expression
If I want to use f in a special program to find its zeroes, how can I convert f to a function? Or, what should I do to find the zeroes of f and such nested expressions?
You have a couple of options...
Option #1: Automatically generate a function
If you have version 4.9 (R2007b+) or later of the Symbolic Toolbox you can convert a symbolic expression to an anonymous function or a function M-file using the matlabFunction function. An example from the documentation:
>> syms x y
>> r = sqrt(x^2 + y^2);
>> ht = matlabFunction(sin(r)/r)
ht =
#(x,y)sin(sqrt(x.^2+y.^2)).*1./sqrt(x.^2+y.^2)
Option #2: Generate a function by hand
Since you've already written a set of symbolic equations, you can simply cut and paste part of that code into a function. Here's what your above example would look like:
function output = f(beta,n1,n2,m,aa)
u = sqrt(n2-beta.^2);
w = sqrt(beta.^2-n1);
a = tan(u)./w+tanh(w)./u;
b = tanh(u)./w;
output = (a+b).*cos(aa.*u+m.*pi)+(a-b).*sin(aa.*u+m.*pi);
end
When calling this function f you have to input the values of beta and the 4 constants and it will return the result of evaluating your main expression.
NOTE: Since you also mentioned wanting to find zeroes of f, you could try using the SOLVE function on your symbolic equation:
zeroValues = solve(f,'beta');
Someone has tagged this question with Matlab so I'll assume that you are concerned with solving the equation with Matlab. If you have a copy of the Matlab Symbolic toolbox you should be able to solve it directly as a previous respondent has suggested.
If not, then I suggest you write a Matlab m-file to evaluate your function f(). The pseudo-code you're already written will translate almost directly into lines of Matlab. As I read it your function f() is a function only of the variable beta since you indicate that n1,n2,m and a are all constants. I suggest that you plot the values of f(beta) for a range of values. The graph will indicate where the 0s of the function are and you can easily code up a bisection or similar algorithm to give you their values to your desired degree of accuracy.
If you broad intention is to have numeric values of certain symbolic expressions you have, for example, you have a larger program that generates symbolic expressions and you want to use these expression for numeric purposes, you can simply evaluate them using 'eval'. If their parameters have numeric values in the workspace, just use eval on your expression. For example,
syms beta
%n1,n2,m,aa= Constants
% values to exemplify
n1 = 1; n2 = 3; m = 1; aa = 5;
u = sqrt(n2-beta^2);
w = sqrt(beta^2-n1);
a = tan(u)/w+tanh(w)/u;
b = tanh(u)/w;
f = (a+b)*cos(aa*u+m*pi)+a-b*sin(aa*u+m*pi); %# The main expression
If beta has a value
beta = 1.5;
eval(beta)
This will calculate the value of f for a particular beta. Using it as a function. This solution will suit you in the scenario of using automatically generated symbolic expressions and will be interesting for fast testing with them. If you are writing a program to find zeros, it will be enough using eval(f) when you have to evaluate the function. When using a Matlab function to find zeros using anonymous function will be better, but you can also wrap the eval(f) inside a m-file.
If you're interested with just the answer for this specific equation, Try Wolfram Alpha, which will give you answers like:
alt text http://www4c.wolframalpha.com/Calculate/MSP/MSP642199013hbefb463a9000051gi6f4heeebfa7f?MSPStoreType=image/gif&s=15
If you want to solve this type of equation programatically, you probably need to use some software packages for symbolic algebra, like SymPy for python.
quoting the official documentation:
>>> from sympy import I, solve
>>> from sympy.abc import x, y
Solve a polynomial equation:
>>> solve(x**4-1, x)
[1, -1, -I, I]
Solve a linear system:
>>> solve((x+5*y-2, -3*x+6*y-15), x, y)
{x: -3, y: 1}