Integration of Known Dataset to specific value
2 次查看(过去 30 天)
显示 更早的评论
Hello,
So I was working on a problem, and while I found a solution that works, its very ineligant and I was wondering out of pure curosity if anyone knows of a better method. I did my best to describe my problem below, and added a photo for better understanding.
I have a large matrix of data (i,j) where each cell holds the density value at a node as it evolves with time where time occupies i and space occupies j. The cell density is held at each cell position (i,j). The x-positions are discretized so that the distance between two nodes is 50ft. My goal is to find the x position where an integral of the density*length = an area. It should be noted that the units are not standard units, but density is cars/ft and length is ft. I want to find when an integrated area is equal to a specific value (in this case = 1) when starting from the right side and working towards the left. I know that this explaination may be hard to follow, so I have included a hand drawn sketch of the situation below. In the photo, I have some values from a single row added because I was doing my own math, but every single row of the matrix holds a series of density values similar to what I have drawn. The slope between two points is not consistent and I am looking for an x position inbetween nodes.
Here is the code I ended up writing:
for h = 1:t_span % does this for each time level
area = 0;
for i = x_span:-1:26
x_1 = x_range(i-1);
x_2 = x_range(i);
y_1 = store_p(h,i-1);
y_2 = store_p(h,i);
x = [x_1; x_2];
y = [y_1; y_2];
Q = trapz(x,y);
area = area + Q;
if abs((area-1))<0.01
if s == 1
first_car_location_1(1,h) = x_1;
elseif s == 2
first_car_location_2(1,h) = x_1;
else
first_car_location_3(1,h) = x_1;
end
break
else
new_length = 45;
if area >= 1
overshoot = area - 1;
for j = 1:25
a_1 = 0;
a_2 = new_length;
a = [a_1;a_2];
b_1 = y_1;
b_2 = (y_2-y_1)*a_2/50 + y_1;
b = [b_1; b_2];
new_area = trapz(a,b);
if new_area - overshoot > 0.001
new_length = new_length-2;
else
if s == 1
first_car_location_1(1,h) = x_range(i-1)+a_2;
elseif s == 2
first_car_location_2(1,h) = x_range(i-1)+a_2;
else
first_car_location_3(1,h) = x_range(i-1)+a_2;
end
break
end
end %end for loop for new length
break % kick out of if area >= 1 statment
end
end
end
end
end
OF NOTE: store_p is the matrix itself, where the dimensions are (time t, position x), t_span is the i dimension length, x_span is the j dimension length. I stop at 26 because in my x_range thats where x becomes negative and thus is a bound of the problem.
If you are interested in why im doing this, its an analysis of the traffic equation for a class.
Again, my solution works and gives me the answers I need, but my answer seems very barbaric, just looking for ways to do something similar in the future a little more neatly.
Thank you, Braden
0 个评论
回答(1 个)
Prudhvi Peddagoni
2020-12-28
Hi,
It basically gives you the vector of all the area values up until that point.
Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!