how to solve this
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 19 Jan 2023
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.
More Answers (1)
Walter Roberson
on 19 Jan 2023
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 Comments
Walter Roberson
on 19 Jan 2023
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, ...)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!