Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My directions were to modify the following matlab code to incorporate a new inpu

ID: 1832066 • Letter: M

Question

My directions were to modify the following matlab code to incorporate a new input variable called 'nstep' and change the programs operation to store and print the output only a few each nstep iterations instead of every time. I'm a little confused as to what this means. Any help would be much appreciated! The code given follows.






time=0;
tstop=input('Enter tstop now.');
m=input('Enter value of m now.');
k=input('Enter value of k now.');
b=input('Enter value of b now.');
dt=input('Enter value of the time step now.');
nstep=input('Enter nstep now.');
pos=input('Enter starting position now.');
vel=input('Enter starting velocity now.');
tic
i=1;
while time<tstop
t(i)=time;
xdot(i)=vel;
x(i)=pos;
acc=-(k/m)*pos-(b/m)*vel;
x2dot(i)=acc;
vel=vel+acc*dt;
pos=pos+vel*dt;
time=time+dt;
i=i+1;
end
out=[t',x2dot',xdot',x']
toc

Explanation / Answer

Ok, I've modified the code you included in your question. The lines I added are lines 11 and 12. Also lines 24 through to 30. These are the line numbers in the modified code below. "change the programs operation to store and print the output only a few each nstep iterations " This means that if you have 100 steps, then you don't print out values at every step, but at every 10th step instead, for example. You choose the value for nstep. The steps at line 24 - 30 allow you to do this. By using a counter, which is incremented, you can determine exactly how often when to output info. Then reset the counter, and count again. etc. In the code below, lines 2 - 9, I have coded in input values for tstop, m, k, b, etc, to save having to input these values each time I ran/tested the code segment. You can modify these lines back to their original form. Now I’m not too sure what you meant by “store and print the output”. By “store”, do you mean save the data to disc? That’s what I’ve done. I’ve opened a file for writing to, and then printed to that file using fprintf(), or you could simply display it onscreen using the statement “out=[t(i),x2dot(i),xdot(i),x(i)];”, or both. I’ve also plotted some of the output; x, xdot, x2dot against time. Just to let you see what is happening. You can remove this I think that’s all. If you have any questions, please ask. Here comes the code. tstop=10;%tstop=input('Enter tstop now.'); m=1;%m=input('Enter value of m now.'); k=4;%k=input('Enter value of k now.'); b=1;%b=input('Enter value of b now.'); dt=0.1;%dt=input('Enter value of the time step now.'); nstep=10;%nstep=input('Enter nstep now.'); pos=0;%pos=input('Enter starting position now.'); vel=5;%vel=input('Enter starting velocity now.'); tic i=1; count=0; fid = fopen('store.txt', 'w'); while time
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote