NEED HELP WITH CODING PROBLEM!
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
*Problem 3
There are several ways to use nested for loops for solving problems. This problem will use nested for loops to create plots of a spring-mass system with four different initial positions and three different initial velocity values. The result will be 12 different displacement traces.
The equation for free vibration of an undamped spring-mass system is: xT=x0*cos(wT)+(v0/w)*sin(w*T)
Here w is the natural frequency, x0 the initial position, and v0 the initial velocity.
Create a MATLAB script with a set of nested for loops. The outer loop will run for the length of the initial position vector and the inner loop will run for the length of the initial velocity vector. For each of the four initial positions, create a new figure to plot all three displacement time traces with that same initial position but different initial velocities. The outer loop thus should contain figure, xlabel, ylabel, title, xlim, ylim, and hold on statements. The inner loop should contain the calculation of x(t), a plot statement, and a pause statement. All four plots should have the same limits so that it is easy to compare values across plots.
Recall from the class discussion that vectors can be used as loop variables, and incorporate this in your solution.
Use the following parameters: 0 = 4.2; % 1/s time = [0:.01:10]; % s x0 = [5.1, 10.2, 24.8, 0.3]; % mm v0 = [37.8, 2.2, 16.1]; % mm/s*
MY CODE SO FAR IS:
w=4.2;
t=[0:.01:10];
for x0=[5.1,10.2,24.8,.3]
for v0=[37.8;2.2;16.1]
x=x0.*cos(w.*t)+(v0./w).*sin(w.*t)
end
end
figure()
hold on
plot(t,x(1,:))
plot(t,x(2,:))
plot(t,x(3,:))
hold off
I don't know how to formulate my code so that it evaluates the velocities for each separate initial x. I'm trying to get a result that will give me x=5.1 distance for velocity 37.8, 24.8, 16.1 x=10.2 distance for velocity 37.8, 24.8, 16.1 x=24.8 distance for velocity 37.8, 24.8, 16.1 x=.3 distance for velocity 37.8, 24.8, 16.1
0 Kommentare
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!