I\'m stuck on a Matlab problem. I keep getting index exeeds matrix dimensions. H
ID: 3665737 • Letter: I
Question
I'm stuck on a Matlab problem. I keep getting index exeeds matrix dimensions. Here is the problem and what I've got so far:
EULER LEAF – on-going] In last week’s homework you moved one particle multiple steps.In this week’s homework you’re going to move a single particle multiple steps, but with differentstep sizes. You’re also going to use a function to calculate one step of the integration. Step sizes: Use step sizes of 0.05, 0.1, and 0.2. Each step size should be plotted on its own row inthe figure. Use a for loop here – do not copy code. As before, you should integrate for a total of3 seconds; this means that each row of plots will have a different number of points.Function: Create a function file to do one integration step. This function takes in x, v, m, and h.Put your force function calculation in the function file. Return the new x, the new v, and f (returnthe force, f, so that you can plot it). You must use a function file, and the force calculation mustbe in that function file, not in the calling script.Creating the plots: Create the same plots as before. You will need to store the x, v, and f values inorder to plot them (the extra credit part from last week). You should be calling plot exactly oncefor each plot.
my function:
function [x1, F, a, v1] = eulerleaf( x,h,v,m )
x1=x+(h.*v);
F=(0.2*sin(x)).*((x.^2)+x); %Force
a=F/m;% acceleration
v1=v+(h.*a);% Velocity after each loop
end
My Script:
x(1)=0; %meters
v(1)=0.5; %meters/second
m=0.3; %kilograms
h=[0.05, 0.1, .2]; %step size (seconds)
t=3 ;%seconds
k=1; % counter
%steps=t/h;
steps=[60,30,15] % steps in 3 seconds
for j=1:3
h(j)
for i=2:steps %creating for loop to calculate the positions for 0.1 second steps from 0 to 3 seconds
[x1(i), F(i), a(i), v1(i)] = eulerleaf(x(i-1),h(j),v(i-1),m);
end
t=linspace(0,3,length(x1)); % time from 0-3 seconds in intervals of total steps
subplot(3,3,k)
plot(t,x1,'*')
xlabel('Time(seconds)')
ylabel('Position(meters)')
title('Time Vs. Position')
subplot(3,3,k+1)
plot(t,v1,'r*')
xlabel('Time(seconds)')
ylabel('Velocity(meters/sec)')
title('Time Vs. Velocity')
subplot(3,3,k+2)
plot(t,F,'g*')
xlabel('Time(seconds)')
ylabel('Force')
title('Time Vs. Force')
k=k+3;
end
Explanation / Answer
Fixed the Index out of bounds error
function [x1, F, a, v1] = eulerleaf( x,h,v,m )
x1=x+(h.*v);
F=(0.2*sin(x)).*((x.^2)+x); %Force
a=F/m;% acceleration
v1=v+(h.*a);% Velocity after each loop
end
%My Script:
x(1)=0; %meters
v(1)=0.5; %meters/second
m=0.3; %kilograms
h=[0.05, 0.1, 0.2]; %step size (seconds)
t=3 ;%seconds
k=1; % counter
%steps=t/h;
steps=[60,30,15] % steps in 3 seconds
for j=1:3
h(j)
for i=2:steps %creating for loop to calculate the positions for 0.1 second steps from 0 to 3 seconds
% x and v are having only one element each. so use x(1) and v(1) instead of x(i-1) and v(i-1)
[x1(i), F(i), a(i), v1(i)] = eulerleaf(x(1),h(j),v(1),m);
end
t=linspace(0,3,length(x1)); % time from 0-3 seconds in intervals of total steps
subplot(3,3,k)
plot(t,x1,'*')
xlabel('Time(seconds)')
ylabel('Position(meters)')
title('Time Vs. Position')
subplot(3,3,k+1)
plot(t,v1,'r*')
xlabel('Time(seconds)')
ylabel('Velocity(meters/sec)')
title('Time Vs. Velocity')
subplot(3,3,k+2)
plot(t,F,'g*')
xlabel('Time(seconds)')
ylabel('Force')
title('Time Vs. Force')
k=k+3;
end