I am trying to do a basic linear equation using the symbolic toolbox. I set my equation, two initial conditions, and solve, but I only get an empty result:
syms y(x) x m c
eqn = y(x) == m*x + c;
cond1 = y(x==0) == 0;
cond2 = y(x==1) == 1;
sol = solve(eqn,cond1,cond2,m,c)
It returns sol.c as 0 but sol.m as y(x)/x
You could transform your conditions into vectors:
a = [0;1];
b = [0;1];
syms m c
eqn = a.*m + c -b ==0;
sol = solve(eqn,m,c)
This will result in a scalar value for m
Related
I have the following equation that I want to solve using MATLAB:
X is the unknown variable. I am trying to solve it using MATLAB solve, but I find it hard to code the left part of the equation.
Is it possible to use solve? Are there any other options?
EDIT
Since A and B depends respectively on j and i I have tried to put them in vectors as follows:
A = [A(1) ... A(j) ... A(N)]
B = [B(1) ... B(i) ... B(N)]
I was trying to have something that looks like this:
eqn = sum(A ./ sum(B .* D)) == C;
solve(eqn);
but the whole difficulty is in this part:
because it depends on both i and j.
To write equation you can use this code:
syms x real
C = 1;
beta = 10;
alph = 0.5;
N = 10;
lenA = N;
lenB = N;
A = rand(1,N);
B = rand(1,N);
eq = 0;
for j=2:N
eqaux = 0;
for i=1:N
eqaux = eqaux+B(i)/((alph+beta*x)^(i+j+1));
end
eq = eq+A(j)/eqaux;
end
eq = simplify(eq==C);
If x must be a complex number, delete real of syms x real.
To solve the equation use this code:
sol = solve(eq,x);
sol = vpa(sol);
Of course yu must use your own values of C, alph, beta, A, B and N.
I am using Matlab to try and solve a system of three first-order ODEs, but the error message I get is 'syms' requires Symbolic Math Toolbox.
Error in spiders (line 1)
syms f(t) s(t) v(t) r W c h q a k b H K e
On a previous occasion, I received an error saying that this system of ODEs cannot be solved explicitly (i.e. in closed form). I think that numerical integration is the only way. The r,W,c,h, etc are parameters. Could someone please tell me how I can simulate/solve and plot the ODEs below?
syms f(t) s(t) v(t) r W c h q a k b H K e
r = 1;
W = 0.5;
c = 0.4;
h = 0.9;
q = 9;
a = 5;
k = 0.8;
b = 6;
H = 3;
K = 1.3;
e = 2;
ode1 = diff(f) == r*f*(1 - f/W) - c*s*f - h*(1 - q)*f;
ode2 = diff(s) == s*(-a + k*b*v/(H + v) + k*c*f) - h*K*q*s;
ode3 = diff(v) == v*(e - b*s/(H + v)) - h*q*v;
odes=[ode1;ode2;ode3]
S = dsolve(odes)
plot(S);
Looks like you where previously using a different MATLAB license, which included the Symbolic Math Toolbox. I assume you no longer have it available and you are now looking for numeric alternatives.
You have to define functions for the equations you are trying to solve. Then you can call one of the ode solvers. An example from the documentation:
y0 = [1; 0; 0];
tspan = [0 4*logspace(-6,6)];
M = [1 0 0; 0 1 0; 0 0 0];
options = odeset('Mass',M,'RelTol',1e-4,'AbsTol',[1e-6 1e-10 1e-6]);
[t,y] = ode15s(#robertsdae,tspan,y0,options);
Where robertsdae is the equation to solve. The full example including further explanations is available here.
I am using subs function in Matlab after I solve for unknown symbols x and y in terms of known symbols a and b. After solving for x and y in terms of a and b I would like Matlab to use a and b instead of x and y. See code below:
clc
clear all
syms a b real;
syms x y real;
eqns = [x + 2*y + 4*b == 5*a;
y == 3*b];
vars = [x,y];
solution = solve(eqns,vars);
x = subs(x,solution.x)
y = subs(y,solution.y)
tst = 5*vars
The output for this code is:
x = 5*a - 10*b
y = 3*b
tst=[ 5*x, 5*y]
I would like to get my last answer in terms of a and b. I assume that the line where I used subs for x and y should change it, but it did not.
Thanks!
You need to update your vars vector after the subs:
clc
clear all
syms a b real;
syms x y real;
eqns = [x + 2*y + 4*b == 5*a;
y == 3*b];
vars = [x,y];
solution = solve(eqns,vars);
x = subs(x,solution.x)
y = subs(y,solution.y)
vars = [x,y];
tst = 5*vars
Introduction
I am using Matlab to simulate some dynamic systems through numerically solving systems of Second Order Ordinary Differential Equations using ODE45. I found a great tutorial from Mathworks (link for tutorial at end) on how to do this.
In the tutorial the system of equations is explicit in x and y as shown below:
x''=-D(y) * x' * sqrt(x'^2 + y'^2)
y''=-D(y) * y' * sqrt(x'^2 + y'^2) + g(y)
Both equations above have form y'' = f(x, x', y, y')
Question
However, I am coming across systems of equations where the variables can not be solved for explicitly as shown in the example. For example one of the systems has the following set of 3 second order ordinary differential equations:
y double prime equation
y'' - .5*L*(x''*sin(x) + x'^2*cos(x) + (k/m)*y - g = 0
x double prime equation
.33*L^2*x'' - .5*L*y''sin(x) - .33*L^2*C*cos(x) + .5*g*L*sin(x) = 0
A single prime is first derivative
A double prime is second derivative
L, g, m, k, and C are given parameters.
How can Matlab be used to numerically solve a set of second order ordinary differential equations where second order can not be explicitly solved for?
Thanks!
Your second system has the form
a11*x'' + a12*y'' = f1(x,y,x',y')
a21*x'' + a22*y'' = f2(x,y,x',y')
which you can solve as a linear system
[x'', y''] = A\f
or in this case explicitly using Cramer's rule
x'' = ( a22*f1 - a12*f2 ) / (a11*a22 - a12*a21)
y'' accordingly.
I would strongly recommend leaving the intermediate variables in the code to reduce chances for typing errors and avoid multiple computation of the same expressions.
Code could look like this (untested)
function dz = odefunc(t,z)
x=z(1); dx=z(2); y=z(3); dy=z(4);
A = [ [-.5*L*sin(x), 1] ; [.33*L^2, -0.5*L*sin(x)] ]
b = [ [dx^2*cos(x) + (k/m)*y-g]; [-.33*L^2*C*cos(x) + .5*g*L*sin(x)] ]
d2 = A\b
dz = [ dx, d2(1), dy, d2(2) ]
end
Yes your method is correct!
I post the following code below:
%Rotating Pendulum Sym Main
clc
clear all;
%Define parameters
global M K L g C;
M = 1;
K = 25.6;
L = 1;
C = 1;
g = 9.8;
% define initial values for theta, thetad, del, deld
e_0 = 1;
ed_0 = 0;
theta_0 = 0;
thetad_0 = .5;
initialValues = [e_0, ed_0, theta_0, thetad_0];
% Set a timespan
t_initial = 0;
t_final = 36;
dt = .01;
N = (t_final - t_initial)/dt;
timeSpan = linspace(t_final, t_initial, N);
% Run ode45 to get z (theta, thetad, del, deld)
[t, z] = ode45(#RotSpngHndl, timeSpan, initialValues);
%initialize variables
e = zeros(N,1);
ed = zeros(N,1);
theta = zeros(N,1);
thetad = zeros(N,1);
T = zeros(N,1);
V = zeros(N,1);
x = zeros(N,1);
y = zeros(N,1);
for i = 1:N
e(i) = z(i, 1);
ed(i) = z(i, 2);
theta(i) = z(i, 3);
thetad(i) = z(i, 4);
T(i) = .5*M*(ed(i)^2 + (1/3)*L^2*C*sin(theta(i)) + (1/3)*L^2*thetad(i)^2 - L*ed(i)*thetad(i)*sin(theta(i)));
V(i) = -M*g*(e(i) + .5*L*cos(theta(i)));
E(i) = T(i) + V(i);
end
figure(1)
plot(t, T,'r');
hold on;
plot(t, V,'b');
plot(t,E,'y');
title('Energy');
xlabel('time(sec)');
legend('Kinetic Energy', 'Potential Energy', 'Total Energy');
Here is function handle file for ode45:
function dz = RotSpngHndl(~, z)
% Define Global Parameters
global M K L g C
A = [1, -.5*L*sin(z(3));
-.5*L*sin(z(3)), (1/3)*L^2];
b = [.5*L*z(4)^2*cos(z(3)) - (K/M)*z(1) + g;
(1/3)*L^2*C*cos(z(3)) + .5*g*L*sin(z(3))];
X = A\b;
% return column vector [ed; edd; ed; edd]
dz = [z(2);
X(1);
z(4);
X(2)];
I'm having some trouble solving this. Matlab always returns a empties syms variables (H, d, k and L), but I know there is a solution because I've already solve it with another program. I need to solve this one at matlab though. Some idea of what I can change?
Thanks!
clear all
syms H d k L
%parameters values
ro = 1020; % kg/m^3 - seawater
g = 9.81; % m/s^2
T = 10; % s
z = d;
s = 0; % =d+z, with z=-d;
teta = 0;
%equations definitions
omega = sqrt(k*g*tanh(k*d));
pressureStokes = -ro*g*z + 0.5*ro*g*H*cosh(k*s)/cosh(k*d)*cos(teta) + 3/4*ro*g*H*pi*H/L/sinh(2*k*d)*(cosh(2*k*s)/sinh(k*d)^2-1/3)*cos(2*teta) - 1/4*ro*g*H*pi*H/L/sinh(2*k*d)*(cosh(2*k*s)-1);
%equations to solve
eq1 = omega == 2*pi/T;
eq2 = k == 2*pi/L;
eq3 = pressureStokes == 86.7e3;
eq4 = pressureStokes == 124.4e3;
[H,d,k,L] = vpasolve([eq1,eq2,eq3,eq4],[H d k L],[5 10 0.07 90]);
The system contains the equations pressureStokes == 86.7e3; and pressureStokes == 124.4e3; that can not be satisfied at the same time