how to solve this
조회 수: 5 (최근 30일)
이전 댓글 표시
p=[101.3 106.6 109.4 114.7 116.9 118.2 123.7];
v=[1.69 3.67 4.37 5.45 5.84 6.05 6.90];
% 4 ways for interpolation
lin=interp1(v,p,5,'linear')
m=interp1(v,p,5,'makima')
sp=interp1(v,p,5,'spline')
n=interp1(v,p,5,'nearest')
% Display a graph of speed versus pressure, the graph will include the given measurements
as well as the results obtained using 4 methods interpolation.
Mark with a red asterisk the points where the speed is 5 [m/s.]
* Labels must be added to the axes, legend, grid lines and title.
댓글 수: 0
채택된 답변
Image Analyst
2023년 1월 19일
You almost got it. Maybe you just needed to read the manual some more and understand what is x and what is y.
p=[101.3 106.6 109.4 114.7 116.9 118.2 123.7];
v=[1.69 3.67 4.37 5.45 5.84 6.05 6.90];
plot(p, v, 'b.-', 'LineWidth', 2, 'MarkerSize', 20);
grid on;
xlabel('Pressure');
ylabel('Velocity');
% 4 ways for interpolation
xq = linspace(min(p), max(p), 500);
lin=interp1(p, v, xq,'linear') ;
m=interp1(p, v, xq,'makima');
sp=interp1(p, v, xq,'spline') ;
n=interp1(p, v, xq,'nearest');
% Find where speed is closest to 5
[value, index] = min(abs(lin - 5)) % Value is how far away from 5 the data point is.
x5 = xq(index)
y5 = lin(index)
hold on;
yline(5, 'Color', 'r') % Horizontal red line at 5.
xline(x5, 'Color', 'r') % Horizontal red line at 5.
% Plot a red asterisk at the point closest to y=5
plot(x5, y5, 'r*', 'MarkerSize', 30, 'LineWidth', 2)
Go ahead and add the rest, like legend, title, etc.
추가 답변 (1개)
Walter Roberson
2023년 1월 19일
When you call plot, you can use
plot(x1, y1, x2, y2, x3, y3, ...)
where x1 and x2 and x3 and so on might happen to all be the same.
For the part about red: see https://www.mathworks.com/help/matlab/creating_plots/create-line-plot-with-markers.html#bvcbmlx-1
댓글 수: 4
Walter Roberson
2023년 1월 19일
plot(v,p,lin,m,sp,n,'r*',5)
That code asks to use v as the first independent variable, and plot p as dependent on it. And then to take lin as the next independent variable and plot m as depedent on it.
Contrast to
plot(v, p, v, lin, v, ...)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
