Array from a script and area under the curve

6 次查看(过去 30 天)
Hi guys I generated this script to plot a graph of velociy over time, no problems with this code. However I am now asked to retrieve the arrays and plot them as a table and calculate the area under the curve.
I think I need to be extracting the resulting arrays and combining them as a function and then invoking this function into a second script designed to calulate the area under the curve. Particularly stuck with extracting the arrays and combining them as a function. Also as this curve is actully just straight lines should I be using the trapz function withing the second script.
Im just abit lost on how to go about this as this is my fourth leasson and these computer labs are difficult with my current lockdown situations. Any hints or tips would be greatfully appreciated thanks.
ps. not looking for a complete answer just a push in the right direction.
%% Part
a=0:0.1:15; % Three arrays for the three different vectors
b=0:0.1:25; % Each array mesures to its alloted variable in 0.1 second increments
c=0:0.1:32.5;
x = 32.5; % assigning pre determined x variable
t = 0:0.1:32.6; % Time array of 1*0.1 increment above the x variable in order to allow the while loop to funtion
i=1;
while t(i)<=15 % Equality condition for the period of time being measured
v(i)=2*t(i); % Velocity 1 formula
i=i+1; % indexing i+1
end
while t(i)>15 && t(i)<=25 % Equality conditon for the period of time being measured
v(i)=30; % Velocity 2 formula
i=i+1; % indexing i+1
end
while t(i)>25 && t(i)<=32.5 % Equality conditon for the period of time being measured
v(i)=-4*t(i)+130; % Velocity 3
i=i+1; % indexing i+1
end
a = [1:326]; % Is the data points between 1:326
plot(a,v,'-','LineWidth',3); % Plots the data and changes the line thickness

回答(1 个)

Chunru
Chunru 2021-8-28
a=0:0.1:15; % Three arrays for the three different vectors
b=0:0.1:25; % Each array mesures to its alloted variable in 0.1 second increments
c=0:0.1:32.5;
x = 32.5; % assigning pre determined x variable
t = 0:0.1:32.6; % Time array of 1*0.1 increment above the x variable in order to allow the while loop to funtion
v = zeros(size(t));
i=1;
while t(i)<=15 % Equality condition for the period of time being measured
v(i)=2*t(i); % Velocity 1 formula
i=i+1; % indexing i+1
end
while t(i)>15 && t(i)<=25 % Equality conditon for the period of time being measured
v(i)=30; % Velocity 2 formula
i=i+1; % indexing i+1
end
while t(i)>25 && t(i)<=32.5 % Equality conditon for the period of time being measured
v(i)=-4*t(i)+130; % Velocity 3
i=i+1; % indexing i+1
end
%a = [1:326]; % Is the data points between 1:326
plot(t,v,'-','LineWidth',3); % Plots the data and changes the line thickness
xlabel('t'); ylabel('v');
% compute the area under the curve
a = trapz(t, v)
a = 637.5000

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by