MATLAB Answers

Unable to perform assignment because the left and right sides have a different number of elements

33 views (last 30 days)
Error appears in Line 41 according to Matlab. I believe it has something to do with putting a matrix as phi_0 into y_dot(2). Can anyone give some advice on how to correct this?
% ode45
% 1. Define Initial Conditions
x_0 = 50*pi/180*randn(2,50);
phi_0 = x_0(1,:);
phi_dot_0 = x_0(2,:);
y_0 = [phi_0; phi_dot_0]; % Initial State Vector
% 2. Initialize Temporal Parameters
t_0 = 0;
t_f = 10; % sec
tspan = [0 10];
% 3. ode45
options = odeset('RelTol', 10^(-12), 'AbsTol', 10^(-12)); % the tolerances tell ode45 how "accurate to be
[t, X] = ode45(@project, tspan, x_0, options);
plot (t, X(:,1)');
%% %% FUNCTIONS %% %%
function y_dot = project(t,y)
%Initialize
y_dot = zeros(2,50);
x_0 = 50*pi/180*randn(2,50);
phi_0 = x_0(1,:);
%Parameters
g = 9.81;
L1 = 0.5;
L2 = 0.5;
omega = 0.5*sqrt(g/L2);
%State Space Representation of System
y_dot(1) = y(2);
y_dot(2) = ((2*(omega.^2)*(cos(phi_0).^2))-(omega.^2)-(g*cos(phi_0)/L2))*y(1);
end

Accepted Answer

Alan Stevens
Alan Stevens on 2 Mar 2021
Like this?
% 1. Define Initial Conditions
N = 50; %
x_0 = 50*pi/180*randn(2,N);
phi_0 = x_0(1,:);
phi_dot_0 = x_0(2,:);
y_0 = [phi_0; phi_dot_0]; % Initial State Vector
% 2. Initialize Temporal Parameters
t_0 = 0;
t_f = 10; % sec
tspan = [0 10];
% 3. ode45
options = odeset('RelTol', 10^(-12), 'AbsTol', 10^(-12)); % the tolerances tell ode45 how "accurate to be
for i = 1:N % Loop N times
[t, X] = ode45(@project, tspan, y_0(:,i), options);
plot (t, X(:,1)); % Assumes you want all 50 plotted on the same graph
hold on
end
grid
xlabel('t'), ylabel('\phi')
%% %% FUNCTIONS %% %%
function y_dot = project(~,y)
%Initialize
y_dot = zeros(2,1);
%Parameters
g = 9.81;
% L1 = 0.5;
L2 = 0.5;
omega = 0.5*sqrt(g/L2);
%State Space Representation of System
y_dot(1) = y(2);
y_dot(2) = ((2*(omega.^2)*(cos(y(1)).^2))-(omega.^2)-(g*cos(y(1))/L2))*y(1);
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!