I have lab test data from a battery discharge curve. The data consistes in 22 points of voltage versus time. In matlab I have traced an interpolation curve through spline interpolation, but I wish to make the derivative of this plot, how can I do this?
enter image description here
Code
x = [0; 3600 ;7200 ;10800; 14400; 18000; 21600; 25200 ;28800;...
32400; 36000 ;39600 ;43200 ;46800; 50400 ;54000; 57600; 61200;...
64800 ;68400 ;72000; 74880];
y = [12.75; 12.40; 12.38; 12.34; 12.30; 12.26 ;12.21 ;12.17 ;...
12.12; 12.07; 12.02 ;11.97 ;11.91 ;11.85; 11.79; 11.72; 11.65;...
11.56 ;11.46 ;11.35 ;11.17; 10.59];
f = fit( x, y,'cubicinterp')
You can use gradient of y/x:
f = fit( x, y,'cubicinterp')
df = gradient(f(x)); % f'x
dx = gradient(x); % dx
dfx = df ./ dx;
plot(x, y, x, dfx);
Related
I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal
I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
If I visualize Z with
imagesc(Z)
I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type
zz = f(xx,yy)
Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?
Thank you for your help.
Instead of bsxfun you can use meshgrid:
% The two parameters needed for the parametric equation
h = linspace(0,2) ;
th = 0:pi/20:2*pi ;
[R,T] = meshgrid(h,th) ;
% The parametric equation
% f(x) Rotation along Z
% ↓ ↓
X = sin(R) .* cos(T) ;
Y = sin(R) .* sin(T) ;
% Z = h
Z = R ;
surf(X,Y,Z,'EdgeColor',"none")
xlabel('X')
ylabel('Y')
zlabel('Z')
Which produce:
And if you want to extract the contour on the X plane (X = 0) you can use contour:
contour(Y,Z,X,[0,0])
Which produce:
This is my code for finding the centered coefficients for lagrange polynomial interpolation:
% INPUT
% f f scalar - valued function
% interval interpolation interval [a, b]
% n interpolation order
%
% OUTPUT
% coeff centered coefficients of Lagrange interpolant
function coeff = lagrangeInterp (f, interval , n)
a = interval(1);
b = interval(2);
x = linspace(a,b,n+1);
y = f(x);
coeff(1,:) = polyfit(x,y,n);
end
Which is called in the following script
%Plot lagrangeInterp and sin(x) together
hold on
x = 0:0.1*pi:2*pi;
for n = 1:1:4
coeff = lagrangeInterp(#(x)sin(x),[0,2*pi],n);
plot(x,polyval(coeff,x,'-'));
end
y = sin(x);
plot(x,y);
legend('1st order','2nd order','3rd order','4th order','sin(x)');
To check for stability I would like to perturb the function (eg g(x) = f(x) + epsilon). How would I go about this?
Well, a little trick for you.
You know randn([m,n]) in matlab generate a m*n random matrix. The point is to generate a random vector, and interp1 to a function of x. Like this:
x = linspace(a,b,n+1); % Your range of input
g = #(ep,xx)f(xx)+interp1(x,ep*randn([length(x),1]),xx);
Take a set of data and plot a curve.
Take the curve and apply curve fitting to generate coefficients.
Use coefficients to generate the curve back via a function.
Currently tried polynomial with nth orders, but is very inaccurate. Also tried spline which is accurate but does not allow the curve to be regenerated only using a function.
I think you are probably looking for a spline fit & method. MATLAB spline objects do allow a curve to be regenerated using only a function (ppval).
e.g.
% original curve 1
x = linspace(0, 4);
y = cos(2 * pi * x);
subplot 221
plot(x, y)
title('Original Curve 2')
% fitted curve 1
p = pchip(x, y);
s = spline(x, y);
subplot 223
plot(x, ppval(p, x), x, ppval(s, x))
legend('pchip','spline')
title('Fitted Curve 1')
% original curve 2
nSample = 1001;
x = linspace(0, 1e-2, nSample);
nPulse = 9;
c = [linspace(0.001, 0.009, nPulse); (7/8).^(1:nPulse)]';
y = pulstran(x, c, #gauspuls, 10000, 0.5);
subplot 222
plot(x, y)
title('Original Curve 2')
% fitted curve 2
p = pchip(x, y);
s = spline(x, y);
subplot 224
plot(x, ppval(p, x), x, ppval(s, x))
legend('pchip','spline')
title('Fitted Curve 2')
I'm using random points to determine the area below a curve (Monte-Carlo):
X: 1xn vector of x values for the function
Y: 1xn vector of y = f(x)
RP: mxn matrix of m random y for each x
I would like to split RP into RPA and RPB depending on it being above or below the curve. The idea is then to plot RPA and RPB against X, in different colors. This code doesn't work because RPA and RPB number of columns is not the same than X:
clf
f = #(x) sin(x/10) + cos(x/60); % Function
xMin = 1; xMax = 100; % x interval
X = [xMin:xMax];
Y = f(X);
plot(X,Y), hold on % Plot function
yMin = min(Y); yMax = max(Y); % Axes limits
set(gca, 'xlim', [xMin, xMax], 'ylim', [yMin, yMax])
m = 20; % Random points per x value
RP = rand(m, columns(X)) .* (yMax-yMin) .+ yMin;
% Split points (doesn't work)
RPA = RP(RP>Y);
RPB = RP(RP<=Y);
br = size(RPB) / size(RP) % Ratio of points below
a = (xMax - xMin) * (yMax - yMin) * br % Area below
% Plot points
plot(X, RPA, 'r.') % Above
plot(X, RPB, 'g.') % Below
Is there a possibility to build RPA and RPB so that they are the same size than RP, with the excluded points y being NaN or something similar, which can be counted and plotted?
You gave a good answer yourself. You can build RPA and RPB with strategic NaNs:
% Split points (works!)
RPA = RP;
RPA(RP<=Y) = NaN;
RPB = RP;
RPB(RPB > Y) = NaN;
And than calculating the ration as the not-NaN:
br = sum(~isnan(RPB)) / sum(~isnan(RP)) % Ratio of points below
I get this nice image:
I'm trying to practice curve fitting on a 2D Gaussian, but in order to do that I need to add random noise to my predefined Gaussian. My first instinct was to cycle through two for loops and create two matrices X and Y with random numbers, but when I tried that (I don't have the code anymore) Matlab wouldn't let me plot the Gaussian because I didn't generate my X and Y values using the meshgrid function. Since I seem to need to use meshgrid, can anyone help me figure out how to generate a random meshgrid so I can add some noise to my Gaussian?
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
%X = 1:1:100;
%Y = 1:1:100;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Add noise now
EDIT: So I found out that rand can return a random matrix which will work with the surf function (for some reason it wasn't working for me earlier though). The result looks something like this: noisy 2D gaussian
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Make some noise
[xRows, xColumns] = size(X);
[yRows, yColumns] = size(Y);
figure(2)
X = -.1 + (.1+.1)*rand(61,61);
Y = -.1 + (.1+.1)*rand(61,61);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z)
But I feel like the Gaussian has largely lost it's typical bell shape and looks more like a slope field than anything. I'm going to try and refine it but I would love any input.
That's what i would do.
amp=1;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
noiseAmp=.1;
x=[-2:.1:2];
y=[-2:.1:2];
%Create two Noise Vectors
noisez1=noiseAmp.*rand(1,length(x));
noisez2=noiseAmp.*rand(1,length(x));
% Make an meshgrid out of the two Vectors
[noiseZ1,noiseZ2]=meshgrid(noisez1,noisez2);
% Add the Meshgrids togehter
Noise=noiseZ1+noiseZ2;
[X,Y]=meshgrid(x,y);
% Add the Noise to the result of Z
Z=amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)))+Noise;
surf(X,Y,Z);
if you just want a 2D plot you can try this
amp=1;
noiseAmp=0.01;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
x=[-5:.01:5];
noiseY=noiseAmp*rand(1,length(x));
y=noiseY+amp*exp(-((x-x0).^2/(2*sigmaX^2)));
plot(x,y);
where noiseAmp is the Amplitude of the noise.
But if you still want to create a 3D plot with the surf() function, you have to add a random meshgrid to the Z result.