Graphs plotting blank - unsure why and how to fix
Show older comments
- mass = 1;
SpringK = 200;
Initial = 1;
staticf = 0.2;
dynamicf = 0.1;
Period = 7;
dt = 0.01;
angle = 30;
g = 9.81;
while 1
choice = input('Enter 1 to view graphs\n\Enter 2 to change parameters\n\Enter 3 to exit\n');
close all
switch(choice)
case 1
calculations(mass, g, angle, dynamicf, staticf, Period, dt, SpringK, Initial, choice );
case 2
while 1
changeparams = input('Which parameter would you like to change?\n\1 for gravity\n\2 for spring constant\n\3 for mass\n\4 for coeffecient of static friction\n\5 for coeffecient of dynamic friction\n\6 for time period\n\7 for the angle in degrees\n\8 for the initial distance to the spring\n\9 for the interval between subsequent calculations\n\ 10 for no more changes\n\Your choice: ');
% User inputs new parameter value
switch(changeparams)
case 1
% Inputs new value for selected parameter
g = input('Enter new value for gravity: ');
case 2
SpringK = input('Enter new value for spring constant: ');
case 3
mass = input('Enter new value for mass: ');
case 4
staticf = input('Enter new value for coeffecient of static friction: ');
case 5
dynamicf = input('Enter new value for coeffecient of dynamic friction: ');
case 6
Period = input(['Enter new value for the ' ...
'time period of simulation: ']);
case 7
angle = input('Enter new value for the angle: ');
case 8
Initial = input(['Enter new value for the distance ' ...
'to the spring: ']);
case 9
dt = input(['Enter new value for the change ' ...
'in time of the iteration: ']);
case 10
% Exits while statement
break
end
end
case 3
close all
break
end
end
function calculations(mass, g, angle, dynamicf, staticf, Period, dt, SpringK, Initial, choice)
i = 3;
u(i-2) = 0;
u(i-1) = 0;
displacement_data(i-1) = 0;
fprintf('time\t\tacceleration\t\tvelocity\t\tdisplacement\n')
for time = 0:dt:Period
if u(i-1) > 0
multiplier = -1;
else
multiplier = 1;
end
if u(i-2) <= 0 && u(i-1) >= 0
fres = (mass*g*(sind(angle)) - (staticf*cosd(angle)));
elseif (u(i-2) >= 0 && u(i-1) >= 0 && displacement_data(i-1) < Initial) || (u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) < Initial)
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle)));
elseif u(i-2) >= 0 && u(i-1) <= 0
fres = mass*g*(sind(angle) + staticf*cosd(angle)) - SpringK*(displacement_data(i-1) - Initial);
elseif u(i-2) >= 0 && u(i-1) >= 0 && displacement_data(i-1) >= Initial || u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) >= Initial
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle))) - SpringK*(displacement_data(i-1) - Initial);
end
u(i) = (u(i-1)) + (fres/mass * dt);
displacement_data(i) = displacement_data(i-1) + (u(i) * dt);
i = i + 1;
fprintf('%.4f\t\t%.4f\t\t%.4f\t\t%.4f\n', time, fres/mass, u(i-1), displacement_data(i-1))
end
if choice == 1
plot(time, displacement_data);
title('Displacement-time graph');
xlabel('Time (s)');
ylabel('Displacement (m)');
figure;
plot(time, u);
title('Velocity-time graph');
xlabel('Time (s)')
ylabel('Velocity (m/s)')
figure;
plot(time, fres/mass);
title('Acceleration-time graph')
xlabel('Time (s)')
ylabel('Acceleration (m/s^2)')
end
end
1 Comment
Start with explaining, what you see. What does "Graphs plotting blank" mean? Then explain, what you want to see instead. The readers cannot guess this detail.
By the way, the close all are completely meaningless here. Avoid inserting some cargo cult code lines.
Answers (1)
I guess, the problem is here:
for time = 0:dt:Period
...
end
plot(time, displacement_data);
Now the value of time is the last value of the for loop and a scalar. I assume you want this:
timeV = 0:dt:Period
for time = timeV
...
end
plot(timeV, displacement_data);
By the way, using a text based input interface to define the data is prone to typos and you cannot reproduce the data without a time-consuming editing. It is much easier to use the editor as interface and create a small script or function, to define the parameters. This makes editing and storing the inputs much easier:
function Trial1
mass = 1;
SpringK = 200;
Initial = 1;
staticf = 0.2;
dynamicf = 0.1;
Period = 7;
dt = 0.01;
angle = 30;
g = 9.81;
calculations(mass, g, angle, dynamicf, staticf, Period, ...
dt, SpringK, Initial, 1);
end
1 Comment
Cameron Bhamra
on 28 Mar 2019
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!