Matlab error with differential equations and syms - matlab

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

Related

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

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;

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.

Subscripted assignment dimension mismatch in matlab

I executed this code using Feature Matrix 517*11 and Label Matrix 517*1. But once the dimensions of matrices change the code cant be run. How can I fix this?
The error is:
Subscripted assignment dimension mismatch.
in this line :
edges(k,j) = quantlevels(a);
Here is my code:
function [features,weights] = MI(features,labels,Q)
if nargin <3
Q = 12;
end
edges = zeros(size(features,2),Q+1);
for k = 1:size(features,2)
minval = min(features(:,k));
maxval = max(features(:,k));
if minval==maxval
continue;
end
quantlevels = minval:(maxval-minval)/500:maxval;
N = histc(features(:,k),quantlevels);
totsamples = size(features,1);
N_cum = cumsum(N);
edges(k,1) = -Inf;
stepsize = totsamples/Q;
for j = 1:Q-1
a = find(N_cum > j.*stepsize,1);
edges(k,j) = quantlevels(a);
end
edges(k,j+2) = Inf;
end
S = zeros(size(features));
for k = 1:size(S,2)
S(:,k) = quantize(features(:,k),edges(k,:))+1;
end
I = zeros(size(features,2),1);
for k = 1:size(features,2)
I(k) = computeMI(S(:,k),labels,0);
end
[weights,features] = sort(I,'descend');
%% EOF
function [I,M,SP] = computeMI(seq1,seq2,lag)
if nargin <3
lag = 0;
end
if(length(seq1) ~= length(seq2))
error('Input sequences are of different length');
end
lambda1 = max(seq1);
symbol_count1 = zeros(lambda1,1);
for k = 1:lambda1
symbol_count1(k) = sum(seq1 == k);
end
symbol_prob1 = symbol_count1./sum(symbol_count1)+0.000001;
lambda2 = max(seq2);
symbol_count2 = zeros(lambda2,1);
for k = 1:lambda2
symbol_count2(k) = sum(seq2 == k);
end
symbol_prob2 = symbol_count2./sum(symbol_count2)+0.000001;
M = zeros(lambda1,lambda2);
if(lag > 0)
for k = 1:length(seq1)-lag
loc1 = seq1(k);
loc2 = seq2(k+lag);
M(loc1,loc2) = M(loc1,loc2)+1;
end
else
for k = abs(lag)+1:length(seq1)
loc1 = seq1(k);
loc2 = seq2(k+lag);
M(loc1,loc2) = M(loc1,loc2)+1;
end
end
SP = symbol_prob1*symbol_prob2';
M = M./sum(M(:))+0.000001;
I = sum(sum(M.*log2(M./SP)));
function y = quantize(x, q)
x = x(:);
nx = length(x);
nq = length(q);
y = sum(repmat(x,1,nq)>repmat(q,nx,1),2);
I've run the function several times without getting any error.
I've used as input for "seq1" and "seq2" arrays such as 1:10 and 11:20
Possible error might rise in the loops
for k = 1:lambda1
symbol_count1(k) = sum(seq1 == k);
end
if "seq1" and "seq2" are defined as matrices since sum will return an array while
symbol_count1(k)
is expected to be single value.
Another possible error might rise if seq1 and seq2 are not of type integer since they are used as indexes in
M(loc1,loc2) = M(loc1,loc2)+1;
Hope this helps.

Function in Octave does not run

I made the following function in octave for the Householder algorithm:
function [matQ, matR] = HS(A)
[m,n]=size(A);
Q = eye(n);
%Loop
for (k = 1:n)
x=A(k:m, k);
I = eye(m-k+1);
e1 = I(:,1);
if( (sign(x(1)) == 0) )
sinal = 1;
else
sinal = sign(x(1));
end
v = x + sign(x(1))*norm(x)*e1;
Hk = eye(m-k+1) - 2/(v' * v) * (v * v');
Qk = [eye(k-1) , zeros(k-1, m-k+1); zeros(m-k+1, k-1), Hk];
A = Qk*A;
Q = Q*Qk;
endfor
matQ = Q
matR = A
endfunction
But when I call it in octave, I get the following message: 'HS' undefined near line 5, column 1
I have no idea what am I doing wrong, since I've made other functions that works well. I also know that the algorithm works well because I have tested it before.
What am I doing wrong?

How to skip an error inside a loop and let the loop continue

The following is my full code: (Most of it isn't useful for what I'm asking, but I just put in the entire code for context, the part of the code that is causing me trouble is towards the end)
clc
clear
P = xlsread('b3.xlsx', 'P');
d = xlsread('b3.xlsx', 'd');
CM = xlsread('b3.xlsx', 'Cov');
Original_PD = P; %Store original PD
LM_rows = size(P,1)+1; %Expected LM rows
LM_columns = size(P,2); %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
% Start of the outside loop
for k = 1:size(P,2)
P = Original_PD(:,k);
interval = cell(size(P,1)+2,1);
for i = 1:size(P,1)
interval{i,1} = NaN(size(P,1),2);
interval{i,1}(:,1) = -Inf;
interval{i,1}(:,2) = d;
interval{i,1}(i,1) = d(i,1);
interval{i,1}(i,2) = Inf;
end
interval{i+1,1} = [-Inf*ones(size(P,1),1) d];
interval{i+2,1} = [d Inf*ones(size(P,1),1)];
c = NaN(size(interval,1),1);
for i = 1:size(c,1)
c(i,1) = mvncdf(interval{i,1}(:,1),interval{i,1}(:,2),0,CM);
end
c0 = c(size(P,1)+1,1);
f = c(size(P,1)+2,1);
c = c(1:size(P,1),:);
b0 = exp(1);
b = exp(1)*P;
syms x;
eqn = f*x;
for i = 1:size(P,1)
eqn = eqn*(c0/c(i,1)*x + (b(i,1)-b0)/c(i,1));
end
eqn = c0*x^(size(P,1)+1) + eqn - b0*x^size(P,1);
x0 = solve(eqn);
for i = 1:size(x0)
id(i,1) = isreal(x0(i,1));
end
x0 = x0(id,:);
x0 = x0(x0 > 0,:);
clear x;
for i = 1:size(P,1)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
end
x = [x0'; x];
x = double(x);
x = x(:,sum(x <= 0,1) == 0)
lamda = -log(x);
LM_FINAL(:,k) = lamda;
end
% end of the outside loop
The important part of the above loop is towards the end:
x = x(:,sum(x <= 0,1) == 0)
This condition is sometimes not satisfied and hence the variable x is empty, which means LM_FINAL(:,k) = lamda is also empty. When this happens, I get the error:
x =
Empty matrix: 43-by-0
Improper assignment with rectangular empty matrix.
Error in Solution (line 75)
LM_FINAL(:,k) = lamda;
How can I skip this error so that the column for LM_FINAL remains as empty, but the loop continues (so that the rest of LM_FINAL's columns are filled) rather than terminating?
You can use try and catch phrase to explicitly handle errors inside loop (or elsewhere in your code).