How do I find and display specific y-values on a spline plot?

2 ビュー (過去 30 日間)
Dylan
Dylan 2014 年 3 月 7 日
編集済み: Prateekshya 2024 年 10 月 14 日
The task was to take data points, compute a spline of the points, then calculate the first and second derivate of the spline curve. I have found and plotted the curves correctly, but am having trouble with displaying specific values on the derivate curves. How can I input specific x-values in to the first and second derivate curves to give me y-values, then display them?
code:
close all x = linspace(-3, 3, 9); y = [2, 3, 4.1, 4.5, 2, 2, 3, 3, 3];
%x = linspace(0, 4, 6); %y = [2, 1, .4, 0.08, 0, .2];
xx = linspace(-3,4); pp = spline(x,y); yy = ppval(pp, xx); plot(x, y, '.', 'markersize', 10); hold on; plot(xx, yy, 'b');
dpp = fnder(pp); plot(xx,fnval(dpp,xx),'k'); hold on; ddpp=fnder(dpp); plot(xx,fnval(ddpp,xx),'r'); hold on; y_val=fnval(dpp,xx); y2_val=fnval(ddpp,xx); fprintf('At x = %d, f(x) = %.4f, f''(x) = %.4f\n\n',xx,y_val,y2_val)

回答 (1 件)

Prateekshya
Prateekshya 2024 年 10 月 14 日
編集済み: Prateekshya 2024 年 10 月 14 日
Hello Dylan,
To evaluate and display specific y-values for the first and second derivatives of a spline at given x-values, you can use the fnval function to compute these values. Then, you can display the results using fprintf or annotate them directly on your plot. Here is a step-by-step guide on how to modify your code to achieve this:
  • Define Specific x-values: Decide which specific x-values you want to evaluate.
  • Evaluate the Derivatives: Use fnval to compute the values of the first and second derivatives at these specific x-values.
  • Display the Results: Use fprintf to print the results or annotate them on the plot.
Here is how you can modify your code:
close all;
% Define data points
x = linspace(-3, 3, 9);
y = [2, 3, 4.1, 4.5, 2, 2, 3, 3, 3];
% Define a finer grid for plotting the spline
xx = linspace(-3, 4, 100); % Use more points for smoother plots
% Compute the spline
pp = spline(x, y);
% Evaluate the spline and plot it
yy = ppval(pp, xx);
plot(x, y, '.', 'markersize', 10);
hold on;
plot(xx, yy, 'b');
% Compute and plot the first derivative
dpp = fnder(pp);
plot(xx, fnval(dpp, xx), 'k');
% Compute and plot the second derivative
ddpp = fnder(dpp);
plot(xx, fnval(ddpp, xx), 'r');
% Define specific x-values for evaluation
specific_x_values = [-2, 0, 2]; % Example x-values
% Evaluate and display the derivatives at specific x-values
for i = 1:length(specific_x_values)
x_val = specific_x_values(i);
y_val = fnval(pp, x_val);
dy_val = fnval(dpp, x_val);
ddy_val = fnval(ddpp, x_val);
fprintf('At x = %.2f, f(x) = %.4f, f''(x) = %.4f, f''''(x) = %.4f\n', x_val, y_val, ...
dy_val, ddy_val);
% Annotate the plot
plot(x_val, dy_val, 'ko', 'MarkerFaceColor', 'k'); % First derivative
plot(x_val, ddy_val, 'ro', 'MarkerFaceColor', 'r'); % Second derivative
text(x_val, dy_val, sprintf('%.2f', dy_val), 'VerticalAlignment', 'bottom', ...
'HorizontalAlignment', 'right');
text(x_val, ddy_val, sprintf('%.2f', ddy_val), 'VerticalAlignment', 'bottom', ...
'HorizontalAlignment', 'right');
end
hold off;
The output you get looks like:
I hope this helps!

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by