what is the problem in thecode??

global k m1 m2 q1 q2 rw iT;
k=8500; %Stiffness of snatch strap m1=1830; %Mass of bogged car in kg m2=2650; %Mass of 4WD in kg q1=925*(x(2)^2);%bogged car drag in N q2=140*(x(4)^2);%4WD drag in N rw=0.394; %Wheel radius in m iT=15; %Gearbox ratio
t0=0; %Define the starting time of the motion in second tf=20; %Define the ending time of the motion in second x0= [0; 0; 0; 0]; %Define the displacement and velocity of bogged car and 4WD Tf=(Te*iT*4)/(rw*4); %Equation to find total wheel force of 4WD car qT=+q2+BF; %Equation of total drag force
options=odeset('abstol',le-6, 'reltol',le-6); %Define the relative and absolute tolerance [t,x]=ode45('Motion_Function',[t0,tf],x0,options);
figure(1); %Plot a graph in a new window plot(t,x(:,1), 'b',t,x(:,3), 'r'); xlabel('Time (s)'); ylabel('Displacement (m)'); title('Graph of Displacement versus Time'); legend('Displacement of bogged car', 'Displacement of 4WD'); grid on; %Plot the graph of displacement versus time for bogged car and 4WD
figure(2); %Plot a graph in a new window plot(t,x(:,2), 'b',t,x(:,4), 'r'); xlabel('Time (s)'); ylabel('Velocity (m)'); title('Graph of Velocity versus Time'); legend('Velocity of bogged car', 'Velocity of 4WD'); grid on; %Plot the graph of Velocity versus time for bogged car and 4WD Undefined function or variable 'x'

2 Comments

madhan ravi
madhan ravi on 3 Oct 2018
Edited: madhan ravi on 3 Oct 2018
Select the whole code and press the code button {} also upload Motion_Function.
if true
function x_dot= Motion_Function (t , x) %Define the function of Mtion_Function
global k m1 m2 q1 q2 rw iT; %Definition of variable
if t>0 && t<=4 Te=150+150*cos((pi*t/4)+pi); elseif t>4 && t<=12 Te=300; elseif t>12 && t<=16 Te=150-150*cos((pi*(t-12)/4)+pi); elseif t>16 && t<=20 Te=0; end %Use if function to express the engine torque
Tf=(Te*iT*4)/(rw*4); %Equation to find total wheel force of 4WD car
if t<=6||t>8&&t<=13||t>15 BF=0; elseif t>6&&t<=7 BF=4*t-24; elseif t>7&&t<=8 BF=32-4*t; elseif t>13&&t<=14 BF=4*t-52; elseif t>14&&t<=15 BF=60-4*t; end %Use if function to express the break force of bogged car
qT=+q2+BF; %Equation of total drag force
x_dot(1)=x(2); x_dot(2)=-k/m1*(x(1)-x(3))+(Tf+q1)/m1; x_dot(3)=x(4); x_dot(4)=-k/m2*(x(3)-x(1))-qT/m2;
x_dot = x_dot'; end
if true
% global k m1 m2 q1 q2 rw iT;
k=8500; %Stiffness of snatch strap m1=1830; %Mass of bogged car in kg m2=2650; %Mass of 4WD in kg q1=925*(x(2)^2);%bogged car drag in N q2=140*(x(4)^2);%4WD drag in N rw=0.394; %Wheel radius in m iT=15; %Gearbox ratio
t0=0; %Define the starting time of the motion in second tf=20; %Define the ending time of the motion in second x0= [0; 0; 0; 0]; %Define the displacement and velocity of bogged car and 4WD Tf=(Te*iT*4)/(rw*4); %Equation to find total wheel force of 4WD car qT=+q2+BF; %Equation of total drag force
options=odeset('abstol',le-6, 'reltol',le-6); %Define the relative and absolute tolerance [t,x]=ode45('Motion_Function',[t0,tf],x0,options);
figure(1); %Plot a graph in a new window plot(t,x(:,1), 'b',t,x(:,3), 'r'); xlabel('Time (s)'); ylabel('Displacement (m)'); title('Graph of Displacement versus Time'); legend('Displacement of bogged car', 'Displacement of 4WD'); grid on; %Plot the graph of displacement versus time for bogged car and 4WD
figure(2); %Plot a graph in a new window plot(t,x(:,2), 'b',t,x(:,4), 'r'); xlabel('Time (s)'); ylabel('Velocity (m)'); title('Graph of Velocity versus Time'); legend('Velocity of bogged car', 'Velocity of 4WD'); grid on; %Plot the graph of Velocity versus time for bogged car and 4WD end

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 3 Oct 2018
Edited: madhan ravi on 3 Oct 2018
[t,x]=ode45(@Motion_Function,[t0,tf],x0,options);
Use
@Motion_Function
Instead of string
'Motion_Function'

4 Comments

still undefined
Select the whole code and press the code button your code is not readable
function x_dot= Motion_Function (t , x) %Define the function of Mtion_Function
global k m1 m2 q1 q2 rw iT; %Definition of variable
if t>0 && t<=4
Te=150+150*cos((pi*t/4)+pi);
elseif t>4 && t<=12
Te=300;
elseif t>12 && t<=16
Te=150-150*cos((pi*(t-12)/4)+pi);
elseif t>16 && t<=20
Te=0;
end
%Use if function to express the engine torque
Tf=(Te*iT*4)/(rw*4);
%Equation to find total wheel force of 4WD car
if t<=6||t>8&&t<=13||t>15
BF=0;
elseif t>6&&t<=7
BF=4*t-24;
elseif t>7&&t<=8
BF=32-4*t;
elseif t>13&&t<=14
BF=4*t-52;
elseif t>14&&t<=15
BF=60-4*t;
end
%Use if function to express the break force of bogged car
qT=+q2+BF;
%Equation of total drag force
x_dot(1)=x(2);
x_dot(2)=-k/m1*(x(1)-x(3))+(Tf+q1)/m1;
x_dot(3)=x(4);
x_dot(4)=-k/m2*(x(3)-x(1))-qT/m2;
x_dot = x_dot';
end
global k m1 m2 q1 q2 rw iT;
k=8500; %Stiffness of snatch strap
m1=1830; %Mass of bogged car in kg
m2=2650; %Mass of 4WD in kg
q1=925*(x(2)^2);%bogged car drag in N
q2=140*(x(4)^2);%4WD drag in N
rw=0.394; %Wheel radius in m
iT=15; %Gearbox ratio
t0=0; %Define the starting time of the motion in second
tf=20; %Define the ending time of the motion in second
x0= [0; 0; 0; 0]; %Define the displacement and velocity of bogged car and 4WD
options=odeset('abstol',le-6, 'reltol',le-6); %Define the relative and absolute tolerance
[t,x]=ode45(@Motion_Function,[t0,tf],x0,options);
figure(1); %Plot a graph in a new window
plot(t,x(:,1), 'b',t,x(:,3), 'r');
xlabel('Time (s)');
ylabel('Displacement (m)');
title('Graph of Displacement versus Time');
legend('Displacement of bogged car', 'Displacement of 4WD');
grid on;
%Plot the graph of displacement versus time for bogged car and 4WD
figure(2); %Plot a graph in a new window
plot(t,x(:,2), 'b',t,x(:,4), 'r');
xlabel('Time (s)');
ylabel('Velocity (m)');
title('Graph of Velocity versus Time');
legend('Velocity of bogged car', 'Velocity of 4WD');
grid on;
%Plot the graph of Velocity versus time for bogged car and 4WD

Sign in to comment.

Asked:

on 3 Oct 2018

Commented:

on 3 Oct 2018

Community Treasure Hunt

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

Start Hunting!