Plotting data

4 次查看(过去 30 天)
Zuzana
Zuzana 2011-10-8
Hi, I have some numetical discrete data with their derivatives from some other SW. So can I use somehow the values of derivatives to more accurate function plotting? Thank you very much for your help in advance.
  3 个评论
Jan
Jan 2011-10-9
@Elige: SW=software.
Zuzana
Zuzana 2011-10-9
2. Exactly! But how?

请先登录,再进行评论。

采纳的回答

Dr. Seis
Dr. Seis 2011-10-9
This is a possible work around. Instead of rewriting either "polyfit" or "interp1" to take into account both the known Y amplitude data and the known Y_PRIME gradients at locations in X, you can just generate new "known" Y amplitude data at locations in X plus or minus a "dx". Save code below:
function [xi, yi] = add_grad_data_1D(x, y, y_prime, dx)
% Check input / output arguments
if nargin ~= 4
error('Needs 4 input arguments !');
end
if nargout ~= 2
warning('Probably need 2 output arguments !');
end
% Check to make sure sizes of input arguments are correct/consistent
if min(size(x)) ~= 1
error('A row/column vector is expected !');
end
if sum(size(x) == size(y)) ~= 2 || sum(size(x) == size(y_prime)) ~= 2
error('Size of X, Y, and Y_PRIME must be equal !');
end
if length(dx) > 1
dx = dx(1);
end
% compute new xi and yi vectors that take gradient y_prime into account
xi = zeros(length(x)*3,1);
if size(x,1) ~= size(xi,1) && size(x,2) ~= size(xi,2)
xi = xi';
end
yi = xi;
for i = 1 : length(x)
xi((i-1)*3 + 1 : (i-1)*3 + 3) = x(i)-dx : dx : x(i)+dx;
yi((i-1)*3 + 1 : (i-1)*3 + 3) = y(i)+(-dx : dx : dx)*y_prime(i);
end
Let's say you have 2 points corresponding to f(x) = x^2:
x = [1, 4];
y = [1, 16];
y_prime = [2, 8];
Then choose an increment in x (i.e., dx = 0.1) and run the code above to get:
[xi, yi] = add_grad_data_1D(x, y, y_prime, dx)
xi =
0.9000 1.0000 1.1000 3.9000 4.0000 4.1000
yi =
0.8000 1.0000 1.2000 15.2000 16.0000 16.8000
Then you can run something like "interp1" to get values along a regular grid from 1 to 4 that go exactly through your "known" points:
xk = 1:dx:4;
yk = interp1(xi, yi, xk, 'cubic');
You will notice that you get a much more closer fit to x^2 by including those points, because it forces the cubic spline to go through the points you generated from the gradient info:
plot(xk,yk,'b-',xk,xk.^2,'r-',xk,interp1(x,y,xk,'cubic'),'k--');
legend('Y and Y-PRIME info', 'Actual', 'Only Y info')
If your data have errors (like most data do), then you may not want to fit the curve exactly to each point. In that case you should use something like "polyfit" to determine the best n-order polynomial, in a least-squares sense.
  1 个评论
Zuzana
Zuzana 2011-10-9
Thank you very much, it is really helpfull... I'll try it tomorrow, thank you!!!

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2011-10-9
Yes, the derivatives can be used to increase the accuracy.
You can use the derivatives to determine the coefficients of a polynomial, which matchs the points and the derivatives. If you have a large number of points, this should be done piecewise, e.g. for chunks of 2, 3, or some more points.
A more detailed answer is possible, if you post more details of the problem.
  3 个评论
Zuzana
Zuzana 2011-10-9
In fact I have only 10 points, in all of them I have the value of function and the value of derivative of function. Now I am working only with values of function and I use polyval and polyfilt but I have some problems with degree of polynomial function because I have row idea how the function should looks like...
Zuzana
Zuzana 2011-10-9
No, it is not partial derivatives. It is the function of only one variable. I solved beam problem together with some optimization issues by FEM method, so I have two numbers in every node - deflection (value) and rotation (derivative).

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by