Equating symbolic coefficients - matlab

I would like to seek y particular of ODE y'' - y' - 2y = 4x^2
I made the following script:
syms x A0 A1 A2
ypa = A2*x^2+A1*x+A0; % y_p assume
cyp = diff(ypa,2) - diff(ypa) - 2*ypa % according to ODE
P1 = 4*x^2; P2 = cyp ; % Equating P1 and P2
C = coeffs(P1 - P2,x);
A0 = solve(C(1),A0)
A1 = solve(C(2),A1)
A2 = solve(C(3),A2)
I got the correct answer for A2 = -2. But I did not get for A0 (should be -3) and A1 (should be 2). How to get them automatically?
P.S I'm using MATLAB R2013a.

Instead of calling solve 3 times, once on each equation of C, you should call it once on the entire system of equations so that the proper substitutions are done to give you a numeric result for each variable:
>> [A0, A1, A2] = solve(C)
A0 =
-3
A1 =
2
A2 =
-2

Related

Return vector from ODE45 has shorter length than initial conditions vector, while trying to solve coupled diffEQ

I am trying to use ODE45 to find the solution to 2 rotating bars, rotating on vertical plane, that have a torsional spring that creates a moment on the bars only when the angle between them differs from 90 degrees. I am just using a1-b4 as the constants in the diffEQ and are just imputing their values into a matrix before sending it into the function. I keep betting back an error saying that I am sending 6 initial conditions, but only get 5 back from the ODE45 function. Any ideas on how to fix this?
%system1.m
function [dx] = system1(t,x,parameters)
dx = zeros(4,1);
a1 = parameters(1);
a2 = parameters(2);
a3 = parameters(3);
a4 = parameters(4);
b1 = parameters(5);
b2 = parameters(6);
b3 = parameters(7);
b4 = parameters(8);
dx(1) = x(2); %dtheta1 = angular velocity1
dx(2) = x(3); %d(angular velocity1) = angular acceleration1
dx(4) = x(5); %dtheta2 = angular velocity2
dx(5) = x(6); %d(angular velocity2) = angular acceleration2
dx(2) = a1*x(1)+a2*x(4)+a3*x(2)+a4*x(5); %motion equation 1
dx(5) = b1*x(1)+b2*x(4)+b3*x(2)+b4*x(5); %motion equation 2
%CA2Lou.m
%set parameters
clear;
a1 = -12;
a2 = 12;
a3 = 0;
a4 = 0;
b1 = 4;
b2 = -4;
b3 = 0;
b4 = 0;
parameters = [a1 a2 a3 a4 b1 b2 b3 b4];
%set final time
tf = .5;
options = odeset('MaxStep',.05);
%setting initial conditions
InitialConditions = [90 0 0 0 0 0];
[t_sol,x_sol] = ode45(#system1,[0 tf],InitialConditions,[],parameters);
Your size and indexing for dx don't match x. You initialize dx to 4 elements, even though x has 6. Then you assign values to 4 indices of dx (specifically, [1 2 4 5]) which results in a new size for dx of 5 elements, still one less than the 6 it expects.
You probably need to initialize dx like so:
dx = zeros(6, 1);
Then, your first and second motion equations should probably (I'm guessing) be placed in indices 3 and 6:
dx(3) = a1*x(1)+a2*x(4)+a3*x(2)+a4*x(5); %motion equation 1
dx(6) = b1*x(1)+b2*x(4)+b3*x(2)+b4*x(5); %motion equation 2

Matlab: Estimating coefficients of nonlinear differential equations

Need to solve the system of nonlinear differential equations:
x1p = a1*u2*x1^1.3 + a2*u1 + a3*u3
x2p = (a4*u2 + a5)*x1^1.3 + a6*x2
x3p = (a7*u3 + (a8*u2-a9)*x1)/a10
x1p, x2p & x3p are time derivatives of x1, x2 & x3, i.e. dx1/dt, dx2/dt & dx3/dt.
we have descrete data of x1, x2 & x3 as well as of u1, u2 & u3. We need to solve the problem in order to get the unknown coefficients, a1, a2, …, a10.
Have checked many posts and can say the solution involves ODE45 (or other ODEX) and probably fsolve or fminsearch (Matlab), but have not managed to setup up the problem correctly, guess we don't understand the coding well. Please, suggestions.
you should replace x1p, x2p ,and x3p by using definition of derivative:
x1p = (x1(i+1) - x(i))/ dt , and like this for the others.
then use folowing algorithm (it is not complete):
descrete data of x1, x2 & x3 as well as of u1, u2 & u3
dt = 0.01
myFun = #(a,x1,x2,x3,u1,u2,u3)
[ (x1(i+1) - x1(i))/ dt = a(1)*u2(i)*x1(i)^1.3 + a(2)*u1(i) + a(3)*u3(i);
(x2(i+1) - x2(i))/ dt = (a(4)*u2(i) + a(5)*x1(i)^1.3 + a(6)*x2(i);
(x3(i+1) - x3(i))/ dt = (a(7)*u3(i) + (a(8)*u2(i)-a(9))*x1(i))/a(10) ]
A=[];
a0 = [0; 0; 0 ;0 ;.... ]
for i= 1:1: lenngth(x1)
a=fsolve(#(a)myFun(a,x1,x2,x3,u1,u2,u3),a0,options);
a0 = [ a(1,1) ; a(2,1); a(3,1) ; .......]
A = cat(1,A,a) ;
end
a1 = mean(A(1,:))
a2 = mean(A(2,:))
.
.
a10 = mean(A(10,:))

Parametric Matrix Multiplication

Does matlab supports such multiplication??
I searched a lot and find these
>> X = #(a1,a2,a3,a4)[a1 a2;a3 a4];
>> Y = #(b1,b2,b3,b4)[b1 b2;b3 b4];
>> % Something like ==> X*Y
But this just solves an equation with "value" and does not solve parametric for me. Does matlab support such a multiplication?
Maybe more of a long comment than an answer, but are you looking for symbolic variables? It requires the Symbolic Math Toolbox.
Example:
clc
clear
syms a1 a2 a3 a4 b1 b2 b3 b4
A = [a1 a2;a3 a4]
B = [b1 b2;b3 b4]
C = (A*B)
C =
[ a1*b1 + a2*b3, a1*b2 + a2*b4]
[ a3*b1 + a4*b3, a3*b2 + a4*b4]
Is this what you mean by "parametric matrix"?

Plotting parameterized solutions in matlab

I need to plot parameterized solutions for the following systems of equations using t values from 0 to 0.3 incremented by 0.001 each time:
x′  =  12.3 x  −  2.7 y
y′  =  5.7 x  −  3.7 y
This is what I have so far, but I'm pretty sure my parametric curves are wrong. I'd be expecting some exponential looking thing, not a lot of straight lines. What am I doing wrong?
A = [ 12.3, -2.7; 5.7, -3.7 ]; %initial matrix
[P D] = eig(A); %finding eigenvalues and eigenvectors
i = [1;4.3]; %initial conditions
H = inv(P)*i; %solving for c1 and c2
t = 0:0.001:0.3;
c1 = 0.2580; %constant
c2 = 4.2761; %constant
B1 = [0.9346;0.3558]; %eigenvector
B2 = [0.1775;0.9841]; %eigenvector
a = 11.2721; %eigenvalue
b = -2.6721; %eigenvalue
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
plot(x1,x2);
Your problem was calculating x1 and x2. Since B1 and B2 are vectors, doing this:
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
made x1 and x2 2 by 301 matrices.
The correct result is simpler:
x = c1*B1*exp(a*t) + c2*B2*exp(b*t);
and plotting it gives:
plot(x(1,:),x(2,:));

matlab: large system of coupled nonlinear equations

I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus