I find examples the best way to demonstrate my question. I generate some data, add some random noise, and fit it to get back my chosen "generator" value...
x = linspace(0.01,1,50);
value = 3.82;
y = exp(-value.*x);
y = awgn(y,30);
options = optimset('MaxFunEvals',1000,'MaxIter',1000,'TolFun',1e-10,'Display','off');
model = #(p,x) exp(-p(1).*x);
startingVals = [5];
lb = [1];
ub = [10];
[fittedValue] = lsqcurvefit(model,startingVals,x,y,lb,ub,options)
fittedGraph = exp(-fittedValue.*x);
plot(x,y,'o');
hold on
plot(x,fittedGraph,'r-');
In this new example, I have generated the same data but this time added much more noise to the first 15 points. Because it is random sometimes it works out okay, but after a few runs I get a good example that illustrates my problem. Same code, except for these lines added under value = 3.82
y = exp(-value.*x);
y(1:15) = awgn(y(1:15),5);
y(15:end) = awgn(y(15:end),30);
As you can see, it has clearly not given a good fit to where the data seems reliable, because it is fitting from points 1-50. What I want to do is say, okay MATLAB, I can see we have some noisy data but it seems decent over a range, only fit your exponential from points 15 to the end. I could go back to my code and update it to do this, but I will be batch fitting graphs like this where each one will have different ranges of 'good' data.
So what I am after is a GUI callback mechanisms that allows me to click on two circles from the data and have them change color or something, which indicates the lsqcurvefit will only fit over that range. Internally all it has to change is inside the lsqcurvefit call e.g.
x(16:end),y(16:end)
But the range should update depending on the starting and ending circles I have clicked.
I hope my question is clear. Thanks.
You could use ginput to select the two points for your min and max in the plot.
[x,y]=ginput(2);
%this returns you the x and y coordinates of two points clicked after each other
%the min point is assumed to be clicked first
min=[x(1) y(1)];
max=[x(2) y(2)];
then you could fit your curve with the coordinates for min and max I guess.
You could also switch between a rightclick for the min and a leftclick for the max etc.
Hope this helps you.
Related
I have this equation that I need to simulate and then plot Y in terms of X. I use the following code to do this but at the end it gives me this straight line as the graph which clearly isn't what I expect to see:
r = .5;
beta = 5;
b = 1;
N = 10;
K = 15;
p = .7:.05:5.7;
l_0 = 0:.01:1;
p*K.*(1-(l_0/r)) == 1./((N*beta*(b^beta)./((beta-1)*l_0))).^(1/(beta-1));
plot(p,l_0,'b*-')
I need to see how l_0 varies as p varies by simulating the equation and using the parameter values above.
This is the graph that I get when I run the code:
I somehow guess that there's something wrong with the way I've set the values for p and l_0 but I'm almost new to MATLAB and don't know how to fix it. I'd appreciate if any one could help me to find out where I'm making mistake(s).
If you view the MATLAB Documents page for the colon operator, you will see that : acts as a unit vector. Basically, it creates a linear space from j to k, by i intervals. So the graph as it is currently displayed is correct. It displays two linear vectors.
However, what you may want to change is the aspect ratio of the graph. Right now, your aspect ratio custom fits your data (which it sounds like you do not want). Look under the axis style section in the MATLAB Documentation for axis limits and aspect ratios to see what style you want your plot in.
Hope this helps you out.
The graph/plot that you are showing here is seems to work only with this:
p = .7:.05:5.7;
l_0 = 0:.01:1;
plot(p,l_0,'b*-')
YES! that is it. All the other things that you have written are playing no role (it seems).
basically you have not evaluated / populated p with l_0 so if you want to see that then
keep p blank
Rearrange your equation with only p at LHS.
Let l_0 handle/fill the values of p.
Now if you only want to see the curve/plot between p = .7:.05:5.7;, then you can check how to do that with the axes property or simply zoom-in/out.
Hope this helps
I have this function below and I need to calculate the minimum and maximum of this function U, and also plotting the maximum and minimum value in 3D graph together with the function.
How can I write the code?
[x,y]=meshgrid(0:0.1:pi,0:0.1:2*pi);% x-theta,y-phi
a=90.7;b=36.2;c=12.9;
E=1.44;
U=-E.^2*(a.*sin(x).^2.*cos(y).^2+b.*sin(x).^2.*sin(y).^2+c.*cos(x).^2);
meshc(x,y,U)
xlabel('\theta')
ylabel('\Phi ')
zlabel('U')
I tired this way to find max but I don't know if i did it correct
max(U,[],1) %max row-wise
max(U,[],2) %max column-wise
and for the minimum it didn't work the same idea, also I didn't get the exact value of the maximum
As stated above, to simply find the maximum/minimum of your sampled function, use m = min(U(:)); M = max(U(:)). To be able to plot them, what you are missing are the coordinates that give you those values. For this, you will need the second output of the min/max functions, which gives you the index where the extreme happens.
A possible implementation (possibly not the best one) would be (might not work perfectly, I don't have matlab at hand):
[Ms,I] = max(U,[],1); %row-wise maximum and their indexes
[M,j] = max(Ms); %maximum among all rows and its index
Now i = I(j) is the location of the maximum. You can finally do plot3(x(i,j),y(i,j),U(i,j),'ro') to plot a big red circle in the maximums location, or whatever you like.
Note: I might have it backwards and it might be x(j,i), and so on. Just check. Of course you can do the same thing for min().
EDIT: I just remembered the ind2sub function , which solves all your problems. Following the syntax used above:
[M,ind] = max(U(:));
[i,j] = ind2sub(size(U),ind)
The rest holds the unchanged.
You can simply use something like
max(max(U))
this will find the maximum for your 2D matrix.
For the minimum you just have to replace max with min.
I am currently running improfile on MATLAB with a line going through the center of the picture below:
After doing so, I'm plotting the resultant using the code below:
xi = [1 size(d_Img,2) size(d_Img,2) 1];
yi = [ceil(size(d_Img,1)/2), ceil(size(d_Img,1)/2), ceil(size(d_Img,1)/2 ),ceil(size(d_Img,1)/2)];
c_d = improfile(d_Img,xi,yi);
c_c = improfile(c_Img,xi,yi);
c_d = c_d';
c_c = c_c';
size_c = size(c_d);
n = 1:size_c(2);
plot(n,c_d);
And here is the plot:
Why are the curves mirroring each other? I am asking to gain a better understanding of what exactly improfile seems to be achieving in MATLAB.
improfile computes something like a "path integral", it gives you the image intensity values around a user specified path. For example, if you use:
improfile(img,[1 1],[1 size(img,2)]);
It gives the same as img(:,1). This is because the path you are using in improfile is from (1,1) to (1,size(img,2)) , meaning the first row. However you could definitely add more complicated paths.
In your case you are going trow a path defined by 4 points. The points are, if I substitute your equation by the resultant numbers:
(1,79)->(134,79)->(134,79)->(1,79). Thus, looking at this, it is obvious that your result should be mirrored, because you are integrating over a line the way there, and back!
Sidenote:
you can do plot(c_d); and forget about that n
I am working with some antarctic DEM data in Matlab. So, far I have been able to generate a nice looking mesh, with the following basic code:
load (Data.xyz)
X = Data(:,1);
Y = Data(:,2);
Z = Data(:,3);
xr = unique(X);
yr = unique(Y);
gz = zeros(length(yr),length(xr));
gz = griddata(X,Y,Z,xr,yr');
figure
mesh(xr,yr,gz);
hold on
contour3(xr,yr,gz,'k-');
hold off
Now I have a few questions, which I have not been able to answer despite being at it since past couple of days and looking at all forums and googling day and night. I hope you all experts might be able to suggest me something. My questions are:
The above code takes a lot of time. Agreed that the DEM for antarctica is large sized and slow response time for a code does not necessarily mean that its incorrect. However, I am totally unable to run this code on my laptop (2.5 GHz/4GB) - its so slow. I am wondering if there are other ways to generate mesh which are faster and more efficient.
The second issue is that the above "Data.xyz" contains DEM data from all antarctica. After generating a mesh, I want to clip it based on locations. Say, for e.g., I want to extract mesh data for area bound by x1,y1, x2,y2, x3, y3, and x4,y4. How do I go about doing that? I could not find a suitable function or tool or any user script anywhere which will allow me to do so. Is it possible to cut a mesh in matlab?
I am running Matlab 2012a, and I do not have access to mapping toolbox. Any suggestions???
1.I'd just like to clarify what it is you want your code to do. For the data X,Y,Z, I would assume these are points (X,Y) (not sampled on a grid) each associated with some elevation Z.
When you call
gz = griddata(X,Y,Z,xr,yr');
You are saying that every possible pair (xr(i),yr(j)) are the locations on your grid where you want to sample the surface to create your mesh. I could be wrong but I don't think this is what you want?
I think you would rather sample at equally spaced points, instead using something like...
xr = min(X):spacing:max(X);
yr = min(Y):spacing:max(Y);
gz = griddata(X,Y,Z,xr,yr'); % ' is for the transpose
mesh(xr,yr,gz);
Where spacing a reasonable number for the scale of your data. The way your code is now, you could be taking far more samples than you want, which could be why your code takes so long.
For 2. I think you could get a for loop to just add the points insde the region of interest to three new lists of X,Y,Z values. If your region is rectangular bounded by x_left,x_right and y_left,y_right ...
Xnew = []; Ynew = []; Znew = [];
for i = 1:length(X)
if ( x_left<X(i) )&&( X(i)<x_right )&&( y_left<Y(i) )&&( Y(i)<y_right )
Xew = [Xnew, X(i)];
Ynew = [Ynew, Y(i)];
Znew = [Znew, Z(i)];
end
end
I need to show 1st 10 eigenfaces using PCA for a image feature vector matrix.
I am using following matlab code to create 1st eigenface but I am getting very dark and not so correct eigenfaces.
eFea is a matrix of 240x4096 where each row represents an image of 64x64
newData = eFea';
data = newData;
[M,N] = size(data);
mn = mean(data,2);
data = double(data) - repmat(mn,1,N);
% construct the matrix Y
Y = data' / sqrt(N-1);
% SVD
[u,S,PC] = svd(Y,0);
imshow(reshape(PC(1,:),64,64))
any hints regarding the error in code will be helpful.
IMSHOW does not automatically scale the image. Thus, if you only have values from, say, 0 to 0.3 in the eigenface, everything will be really dark. Try imshow(reshape(PC(1,:),64,64),[]) instead.
This is a really old topic but I want to answer something anyway.
Honestly, I think the error is somewhere else, although what Jonas said might give good-looking results.
You need to add the mean of the data again in the end. I just had the same problem with the dark principal components, that's why I found this question. But then I realized, that when you do PCA, you substract the mean first. That means that in the end, you need to add it again.