Project

Project

%ode_function

function [dtheta_dt] = ode_func(t,theta, l, m, g)

theta1 = theta(1);

theta2 = theta(2);

dtheta1_dt = theta2;

dtheta2_dt = -(g/l)*sin(theta1);

dtheta_dt = [dtheta1_dt; dtheta2_dt];

end

%motion of the pendulum

close all;

clear all;

clc;

%inputs of the pendulum problem

%g = gravity

g = 9.81;

%l = length of the pendulum

l = 1;

%m = mass of the ball -- mass is redundant in calculations

m = 0.5;

theta_0 = [0,3];

%time plot

t_span = linspace(0,10,300);

% solving differential equations

[t, Results] = ode45(@(t,theta) ode_func(t,theta, l, m, g), t_span, theta_0);

figure(1)

plot(t, Results(:,1));

hold on;

plot(t, Results(:,2));

grid on;

ylabel(\'Velocity - Red, Displacement - Blue\');

xlabel(\'Time\');

figure(2)

alpha = Results(:,1);

x0 = 0;

y0 = 2;

% creating motion using loop

ct=1;

for i = 1:length(t_span) ALPHA = alpha(i);

x1 = l*sin(ALPHA);

y1 = l*(1-cos(ALPHA));

clf(\'reset\')

plot([x0 x1],[y0 y1],\'linewidth\', 3);

hold on;

plot(x1,y1,\'*\',\'color\',\'r\', \'linewidth\', 15);

axis([-2 2 -1 2]); pause(0.1);

M(ct)= getframe(gcf);

ct = ct+1;

end

%movie(M) create a movie file for display in MATLAB

movie(M)

videofile = VideoWriter(\'Pendulum_Motion.avi\',\'Uncompressed AVI\');

open(videofile);

writeVideo(videofile,M);

close(videofile);

Dont post the code with errors

Undefined function or variable \'t_span\'.

Error in tfg (line 29)
[t, Results] = ode45(@(t,theta) ode_func(t,theta, l, m, g), t_span, theta_0);

>>can you please check again there was an enter missing caused by typo which the code read it as a comment

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.