Matlab GUI ODE solver 'Value' must be a double scalar - matlab

I am trying to make an 2nd order ODE solver but it's not working (Error using matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (line 111)
'Value' must be a double scalar.) when I run it on the normal matlab (not the app designer), I get the right answer.
m1 = app.m.Value;
c1 = app.c.Value;
k1 = app.k.Value;
syms y(x)
Dy = diff(y);
ode = m1*diff(y,x,2) == app.u.Value - c1*Dy - k1*y;
cond1 = y(0) == 1;
cond2 = Dy(0) == 0;
conds = [cond1 cond2];
ySol(x) = dsolve(ode,conds);
ySol = simplify(ySol);
app.EditField.Value = ySol;

Related

ODE SYSTEM OF EQUATION

I'm trying to solve a system of ode's using Runge-kutta, i made a function for RK2(f,h,x0,y0,xfinal) and tried to solve the system shown below with specified IC's. Could someone help fix the code as I get errors and code doesn't work.
ode set
beta = 1/3;
gamma = 1/7;
syms R S I % Symbolic Math Toolbox
N = S+I+R;
ode1 = -(beta*I*S)/N;
ode2 = -(beta*I*S)/N-gamma*I;
ode3 = gamma*I;
odes = [ode1,ode2,ode3];
for j = odes
RK2(j,0.2,0,8e6,7);
end
function [xs,ys] = RK2(f,h,x0,y0,xfinal)
ffnc = matlabFunction(f);
fprintf('\n x y ');
o = 1;
while x0 <= xfinal
fprintf('\n%4.3f %4.3f ',x0,y0); %values of x and y
xs(o) = x0;
ys(o) = y0;
k1 = h*ffnc (x0,y0);
x1 = x0+h;
k2 = h*ffnc (x1,y0+k1);
y1 = y0+(k1+k2)/2;
x0 = x1;
y0 = y1;
o = o+1;
end
end

Matlab : system of four coupled odes

So, I've been working on this physics problem where I face a system of four coupled differential equations which I can't seem to find the answer to
TT0 = 0; %initial release angle Theta
l0 = 1; %initial left line length (in meters)
m = 1;
M = 3;
R = 0.3;
g = 9.8;
mu = 0.5;
syms t Ti(t) Tx(t) TT(t) l(t);
ode1 = (M*g) - Tx == -M*(diff(l,t,2)+R*diff(TT,t,2));
ode2 = m*g*sin(TT) - Ti == m*(R*diff(TT,t,2)-l*(diff(TT,t))^2 + diff(l,t,2));
ode3 = m*g*cos(TT) == m*(R*(diff(TT,t))^2 + 2*diff(l,t)*diff(TT,t) + l*diff(TT,t,2));
ode4 = Tx == Ti*exp(mu*(pi/2 + TT));
odes = [ode1; ode2; ode3; ode4];
conds = [TT(0) == TT0; l(0) == l0;Ti(0) == 0; Tx(0) == M*g];
D = dsolve(odes,conds);
Here's the code I've written. There are four equations and four indeterminates but I get this error when I run the code:
Error using mupadengine/feval (line 163)
Cannot reduce to the square system because the number of equations
differs from the number of indeterminates.
Error in dsolve>mupadDsolve (line 332)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
Error in Untitled (line 16)
D = dsolve(odes,conds)
I've searched the internet and altered my code several times but it didn't seem to work. I'll highly appreciate any kind of help.

Matlab error with differential equations and syms

I have 2 differential equations that are related to each other by their B.C.
I try to solve them with the following code:
syms v1(x) v2(x) L;
E = 1; %Modulous young
I = 1; %Moment of inertia
P = 1; %Force
Differintial_Equation1 = E*I*diff(v1,x,2) == -(sqrt(2)/2)*P*x;
Differintial_Equation2 = E*I*diff(v2,x,2) == -(sqrt(2)/2)*P*(L-x);
eqs = [Differintial_Equation1, Differintial_Equation2];
Dv1 = diff(v1,x); %v1'
Dv2 = diff(v2,x); %v2'
cond1 = [v1(0)==0, v1(L)+L*cos(pi/4) == v2(0)-L*cos(pi/4)]; %B.C
cond2 = [v2(L)==0, Dv1(L)+(pi/4) == Dv2(0)-(pi/4)]; %B.C 2
conds = [cond1, cond2];
vSol(x) = dsolve(eqs, conds);
I am getting an error:
Error using sym/subsindex (line 766)
Invalid indexing or function definition. When defining a function, ensure
that the arguments are
symbolic variables and the body of the function is a SYM expression. When
indexing, the input must be
numeric, logical, or ':'.
Error in sol_2nd (line 29)
vSol(x) = dsolve(eqs, conds);
If I remove the B.C on Dv and change it to v it works fine, what am I missing here?
Thanks in advance.
In your code dsolvereturns a structure containing the solutions.
You can use [v1sol(x) v2sol(x)]= dsolve(eqs, conds);
So we have this:
syms v1(x) v2(x) L;
E = 1; %Modulous young
I = 1; %Moment of inertia
P = 1; %Force
Differintial_Equation1 = E*I*diff(v1,x,2) == -(sqrt(2)/2)*P*x;
Differintial_Equation2 = E*I*diff(v2,x,2) == -(sqrt(2)/2)*P*(L-x);
eqs = [Differintial_Equation1, Differintial_Equation2];
Dv1 = diff(v1,x); %v1'
Dv2 = diff(v2,x); %v2'
cond1 = [v1(0)==0, v1(L)+L*cos(pi/4) == v2(0)-L*cos(pi/4)]; %B.C
cond2 = [v2(L)==0, Dv1(L)+(pi/4) == Dv2(0)-(pi/4)]; %B.C 2
conds = [cond1, cond2];
[v1sol(x) v2sol(x)]= dsolve(eqs, conds);

Elastic line equation in Matlab - Error using Mupadengine/feval - Error in dsolve>mupadDsolve - Error in dsolve

I can't understand the solution of multiple differential equations in Matlab. In this example, I'm trying to work with independent equations: the program should solve them separately (and it does, see below) and together (it doesn't!), shouldn't it?
N.B. The equations describe the elastic line according to Euler Bernoulli. I'd like to use them, just setting the appropriate intitial conditions and known data for each problem.
This is the part of code in common with both procedures:
Creating symbolic functions
syms v(z) w(z)
syms B Chiterm p mx by l A Eterm p bz
Differentiating
D1v = diff(v,z);
D2v = diff(v,z,2);
D3v = diff(v,z,3);
D4v = diff(v,z,4);
D1w = diff(w,z);
D2w = diff(w,z,2);
Setting known values (and parameters, like p)
by=-p;
Chiterm=0;
mx=0
bz=0;
Eterm=0
Defining differential equations
odetrasv = diff(B*(diff(v,z,2)+Chiterm),z,2)-mx-by == 0;
odelong = diff(A*(diff(w,z)-Eterm),z)+bz == 0;
Defining initial conditions
cond1 = B*D2v(l) == 0;
cond2 = D1v(0) == 0;
cond3 = v(0) == 0;
cond4 = v(l) == 0;
cond5 = D1w(l) == 0;
cond6 = w(0) == 0;
Here is the part of the code that solves the equations separately:
condstrasv = [cond1 cond2 cond3 cond4];
condslong = [cond5 cond6];
vSol(z) = dsolve(odetrasv, condstrasv);
wSol(z) = dsolve(odelong, condslong);
vSol = simplify(vSol)
wSol = simplify(wSol)
Here's the part that is not working. I'm trying to solve them togheter, since the next step is solving a system of multiple equations:
odes = [odetrasv; odelong];
conds = [cond1; cond2; cond3; cond4; cond5; cond6];
[vSol(z), wSol(z)] = dsolve(odes,conds)
Matlab returns:
Error using Mupadengine/feval - Invalid initial conditions
Error in dsolve>mupadDsolve - T=feval(symengine,'symobj::dsolve',sys....
Error in dsolve - sol = mupadDsolve(args, options);
Why are the initial conditions invalid in the second case? Solving without them works:
[vSol(z), wSol(z)] = dsolve(odes)

Why is the MATLAB output of this numerical method precision not getting more accurate?

Posting here vs math.stackexchange because I think my issue is syntax:
I'm trying to analyze the 2nd order ODE: y'' + 2y' + 2y = e^(-x) * sin(x) using MATLAB code for the midpoint method. I first converted the ODE to a system of 1st order equations and then tried to apply it below, but as the discretizations [m] are increased, the output is stopping at .2718. For example, m=11 yields:
ans =
0.2724
and m=101:
ans =
0.2718
and m=10001
ans =
0.2718
Here's the code:
function [y,t] = ODEsolver_midpointND(F,y0,a,b,m)
if nargin < 5, m = 11; end
if nargin < 4, a = 0; b = 1; end
if nargin < 3, a = 0; b = 1; end
if nargin < 2, error('invalid number of inputs'); end
t = linspace(a,b,m)';
h = t(2)-t(1);
n = length(y0);
y = zeros(m,n);
y(1,:) = y0;
for i=2:m
Fty = feval(F,t(i-1),y(i-1,:));
th = t(i-1)+h/2;
y(i,:) = y(i-1,:) + ...
h*feval(F,th,y(i-1,:)+(h/2)*Fty );
end
Separate file:
function F = Fexample1(t,y)
F1 = y(2);
F2 = exp(-t).*sin(t)-2.*y(2)-2.*y(1);
F = [F1,F2];
Third file:
[Y,t] = ODEsolver_midpointND('Fexample1',[0 0],0,1,11);
Ye = [(1./2).*exp(-t).*(sin(t)-t.*cos(t)) (1./2).*exp(-t).*((t-1).*sin(t)- t.*cos(t))];
norm(Y-Ye,inf)
Your ODE solver looks to me like it should work - however there's a typo in the analytic solution you're comparing to. It should be
Ye = [(1./2).*exp(-t).*(sin(t)-t.*cos(t)) (1./2).*exp(-t).*((t-1).*sin(t)+ t.*cos(t))];
i.e. with a + sign before the t.*cos(t) term in the derivative.