recieving error within ODE45: Input arguments to function include colon operator. To input the colon character, use ':' instead.

My code is meant to measure the oscilating path of a pendulum given a series of diferential equations and initial conditions, then the plot the phase plots of the results. However i seem to run into the same error when running the code, the full error message reads: Input arguments to function include colon operator. To input the colon character, use ':' instead.
Error in odearguments (line 75)
y0 = y0(:);
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in lab9main (line 46)
[time, sol] = ode45(pos1, [0 tf], angle, opt1);
hope someone can help! thanks!
clc; clear; close all;
fprintf("Nathan Koch\nProject 9\nPendulum project");
Nathan Koch Project 9 Pendulum project
datetime("today")
ans = datetime
28-Jul-2023
L = 4.5; %meters
Dtheta_0 = 0; %d(theta_0)
theta_0 = pi / 5.5;
g = 9.81; %m/s
k0 = sin(0.5*theta_0); %parameter of k;
C1 = 0; %drag coef case 1
C2 = 0.25; %drag coef case 2
T = @(k,x) 4*sqrt(L/g)*(1./sqrt(1-k.^2*sin(x).^2));%function of k and theta
T0 = @(x) 4*sqrt(L/g)*(1./sqrt(1-k0^2 * sin(x).^2)); %function of theta where k = k0
q = quadgk(T0,0,pi/2); %report value of integral from bounds
fprintf("\n\nthe value of the integral is %.4f\n\n",q);
the value of the integral is 4.3439
%define general equation and initial conditions for use:
angle = @(t) theta_0 + t;
pos1 = @(angle,x) -g/L * sin(angle) - C1*x*abs(x); %second derivative of theta wrt t for case 2
pos2 = @(angle,x) -g/L * sin(angle) - C2*x*abs(x); %second derivative of theta wrt t for case 2
tspan = 0:200;%vector of sample points
tx = 0:0.1:20; %refined points on vector
%Case 1 no drag Cd = 0;
tol = [1e-3, 1e-6];
tf = 200;
print_time = [0, 82.65, 200];
%euler-ing my complex equations
for i = 1:length(tol)
fprintf("using tolerance of %.3e\n",tol(i))
for j = 1:length(print_time)
fprintf("at time step %.2f",print_time(j))
opt1 = odeset("RelTol",tol(j),"AbsTol",tol(j));
[time, sol] = ode45(pos1, [0 tf], angle, opt1);
L1 = interp1(time,sol,print_time(j));
fprintf("ODE45: %.4f at t = %.4f\n",L1,print_time(j));
end
fprintf("\n")
end
using tolerance of 1.000e-03
at time step 0.00
Input arguments to function include colon operator. To input the colon character, use ':' instead.

Error in odearguments (line 75)
y0 = y0(:);

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
for N = 1:length(tol)
fprintf("using tolerance of %.3e\n",tol(N));
for M = 1:length(print_time)
opt2 = odeset("RelTol",tol(M),"AbsTol",tol(M));
[time, sol] = ode45(pos2, [0 tf], angle, opt2);
L2 = interp1(time,sol,print_time(M));
fprintf("ODE45: %,4f at t = %.4f\n",L2,print_time(M));
end
fprintf("\n")
end
%Plot the Phase plots for cases 1 and 2
figure(1)
plot(tspan,thet1,'o', tspan,dtheta1,':.')
xlim([0 200])
ylim([0 20])
title('Plot of Oscilations for non linear pendulum (case 1)')
figure(2)
plot(tspan,thet2,'o',tspan,dtheta2,':.')
xlim([0 200])
ylim([0 20])
title('Plot Oscilations for non linear pendulum (case 2)')

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 28 Jul. 2023
Bearbeitet: Cris LaPierre am 28 Jul. 2023
The specific error is because you are passing in an anonymous function for your initial values rather than values.
Fix this by calling angle at t=0 for your input argument.
[time, sol] = ode45(pos1, [0 tf], angle(0), opt1);
However, that wil just lead to more errors. For example, you are indexing tol using the loop counter for print_time. Since there are more values in print_time than tol, you will get an indexing error from tol(j).

Weitere Antworten (0)

Produkte

Gefragt:

am 28 Jul. 2023

Kommentiert:

am 28 Jul. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by