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

Help! I am supposed to change my code below and use a for loop to match this que

ID: 3664056 • Letter: H

Question

Help!

I am supposed to change my code below and use a for loop to match this question.

%clearing window and variables

clear;

clc;

%time step

h=0.1;

%initial position

x=0:0.4:4;%meters

% initial velocity

v=0.5;%meters/second

%mass

m=0.3;%kg

%a

%force

f=0.2*sin(x).*((x.^2)+x);

%acceleration

a=f/m;

%second position

x2=x+(h*v);%meters

%second velocity

v2=v+(h*a);%meters/second

%b

%second force

f2=0.2*sin(x2).*((x2.^2)+x2);

%second acceleration

a2=f2/m;

%final position

x3=x2+(h*v2);%meters

%final velocity

v3=v2+(h*a2);%meters/second

fprintf('the first particle should end up at %0.1f meters and the last particle at %0.4f meters ',x3(1),x3(11));

Euler leaf problem 3 [multiple time steps]: In the first problem you moved a particle two steps. In this problem you're going to move that particle multiple steps using a for Use a step size h of 0.05 seconds and simulate the particle forward in time for a total of 3 seconds. Start with x-0, m 0.3kg, and v = 0.5 meters/second as before Part a) Print out the location of the particle after 3 seconds Part b) Create a 3-part plot that shows time versus position, time versus velocity, and time versus force (use markers and plot once each time through the for loop). Extra credit[+20]: Create arrays and store the time, position, velocity and force values at every time step so that you can plot all of the values after the for loop has finished. This lets vou plot with lines. Implementation note: Start with one time step from problem 1. Put a for loop around it and change the equations so that you save the new values back to the same variable (ie, x x + hv instead of xnext = xprev + hv) Implementation note extra credit: Create the arrays before the for loops (you know honw big they should be because you know how many steps you want to take). Save the values ofx,v, and F into the array before you calculate the next time step. Note that you can use the arrays directly to store the next and previous values (x(k+1) = x(k) + h * v(k) but it's a bit safer to separate the calculating of the values from the saving of them into arrays.. Hint: x -x+hv takes the old value ofx, adds hv to it, then stores the result back in x. So x always has the current positiorn.

Explanation / Answer

%clearing window and variables
clear;
clc;
%initial position
x=0:0.4:4;%meters
% initial velocity
v=0.5;%meters/second
%mass
m=0.3;%kg
%part a
%force
f=0.2*sin(x).*((x.^2)+x);
%acceleration
a=f/m;
%vectors for x,v,f,a
vecx(0)=x
vecv(0)=v
vecf(0)=f¨
veca(0)=a¨
fprintf('the first particle should end up at %0.1f meters ',vecx(0));
i=1
%b
for h=0.1 to 3, h=h+0.1 %bucle for time step
%second position
vecx(i)=vecx(i-1)+(h*vecv(i-1));%meters
%second velocity
vecv(i)=vecv(i-1)+(h*a);%meters/second
%second force
vecf(i)=0.2*sin(vecx(i-1)).*((vecx(i-1).^2)+vecx(i-1));
%second acceleration
veca=vecf(i)/m;
i=i+1
fprintf('the last particle at %0.4f meters ',vecx(i));
end for

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