I have trying to plot solutions to coupled ODEs.I solve them using ODE15s solver and pass arguments using a for loop.
MATLAB code then returns a set of plots to me(using hold on).Now only one of the given graphs is appropriate to my theory.So,how do I identify the argument(that I had passed through the for loop) which corresponds to my chosen graph(I can pick out the graph visually)
Just use 'text' function to mark the arguments nearby the each graph.There is a very simple demo(a is the argument):
x=0:100;
figure;
hold on;
for a=1:10
plot(x,a*x);
text(100,a*100,sprintf('a=%s',num2str(a)));
end
Sorry for not pasting picture here because my reputation is not enough.
Related
I'm trying to plot a straight line from a point in x to different values of t, thereby making a line in a for loop. But I see no lines generated in my figure in MATLAB
Following is my code:
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(k),t(1:k),'-')
hold on
end
Thanks
You do not need the loop to perform the plot.
plot(x0,t,'-')
Will work just fine! Unless you were attempting to plot points...use scatter() for that:
scatter(x0,t)
plot() and scatter() (and most of Matlab's functions) are meant to be used with vectors, which can take some time to get used to if you are used to traditional programming languages. Just as you didn't need a loop to create the vector x0, you don't need a loop to use plot().
You are adding one point in Y axis along a line in X Axis use this code
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(1:k),t(1:k),'-')
hold on
end
for more fun and see exactly how for is performed use this for loop
for k=1:n
pause('on')
plot(x0(1:k),t(1:k),'-')
hold on
pause(2)
end
I have been trying to create a MATLAB GUI using Guide which :
i) has a function that takes in input parameters (Temperature, Superconducting Gap Energy, Broadening Parameter) and outputs a plot - a Density of States plot showing a superconducting energy gap.
Note: This function involves numerical double integration, so the parameters can not be varied in a straightforward manner by using sliders. To study the variation in the curve from different parameters, each time the new parameters are input, the function double integrates again and a new curve is generated within the same axes.
ii) The GUI has 4 sets of input fields so that 4 curves can be plotted within the same axes at at a given time for comparison.
I used the following code:
handles.P1=plot(handles.dIdV,bias1,cond1,'r','linewidth',2);
%P1 = plot(handles.dIdV,bias1,cond1,'r','linewidth',2);
hold on
%plot(x,data(:,2),'b','linewidth',2)
%scatter(E,XDOS)
%xlim([min(bias) max(bias)])
%title(nem)
(Kindly do not get confused by the comments. 'cond1' is the result of integration for set1 of parameters, this is plotted against the array 'bias'. Thus the DOS (function of bias, temperature, delta-gap value, etc) is integrated up to each 'bias' value and stored in 'cond1', which is then plotted against that 'bias' value. )
to clear this, I wrote:
% --- Executes on button press in clear1.
function clear1_Callback(hObject, eventdata, handles)
% hObject handle to clear1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.temp1=0;
handles.delta1=0;
handles.tao1=0;
handles.vmod1=0;
handles.val1=0;
hold off
i.e. all the parameters are reset to zero and hold function is turned off.
The same bit of code has been written 4 times for 4 different curves.
So each curve has a hold function when the plot command is given and when the clear button is pressed, the 'hold' is supposed to turn off.
e.g. Curve1 is generated using parameterset1. Pressing the clear1 button should turn hold off for curve1 so that new values can be put in parameterset1 and a new curve1 can be plotted on same axes as curve2, curve3 and curve4, without affecting these other curves.
There is a problem with the execution of the 'hold on' and 'hold off' lines in that I cannot delete one curve at a time and replace it with a new one.
The moment I enter new parameters into any of the handles, all the other plots disappear.
Where could I be going wrong?
Is it possible to replace one curve at a time in a single axis with hold on in MATLAB?
Thanks a lot.
Well that is how hold is supposed to work. If you would like to overwrite individual items, just reassign the data that each plot object holds. For example, if you want to replace the handles.temp1 object's data, it would look like:
set(handles.temp1, 'XData', (New xdata), 'YData', (new ydata))
%You might need a drawnow() here but not sure.
You can also update other line object properties the exact same way using set().
Also, note that hold never is off, because otherwise the next plot item will overwrite everything else.
Edit: I would also like to point out that setting handles.temp1 = 0 is not what you are trying to do. Note that a line object has "properties", which is really what you are trying to manipulate. See this for line properties. If you don't know anything about objects and OOP see this
I have written a function of the form
function myplot(x,y)
plot(x,y)
end
This function creates a plot for given values of x and y. The actual function is more complex, but it does not serve the purpose of the question to include its content here. The main point follows.
I have tried to run the following script:
x = [1:0.01:10]
y = [1:0.01:10]
figure
plot(sin([1:0.01:10]))
hold on
myplot(x,y)
The intent here is to plot 2 sets of data on the same graph. The first set of data is generated by Matlab's native plot command while the second set of data is generated by the user custom myplot function (in this case what should be a straight line). The script above wont do it....
How to get Matlab to include both sets of data on the same plot?
your script plots them both but with different x values. if you don't specify x input in plot it uses 1:length(y), while your myplot function does specify x values (which in your case are 10-fold smaller).
just do: plot(x,sin([1:0.01:10])) instead of plot(sin([1:0.01:10]))
You could save the current axes (on which you create the first plot) in a variable and pass it as an argument to your function to be sure it gets plot on the same axes no matter what happens elsewhere in your code.
So your main code could look like this:
x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)
and the function:
function myplot(x,y,hAxes)
plot(hAxes,x,y);
end
I am trying to add Gaussian fit to the histogram in MATLAB, but I do not know how to apply the fit to one particular peak only.
http://postimg.org/image/phms61rdh/ first plot
http://postimg.org/image/sindrn8er/ second plot
I am also posting part of the code I have been using:
data_Rb = (importdata('path\name.txt'));
counts_Rb = data_Rb.data(:,3);
figure
hist(counts_Rb, nbins);
xlim([0 120]);
title('Rubidium');
histfit(counts_Rb,1000);
pd=fitdist(counts_Rb(:),'normal')
x=0:0.001:120;
PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit
PDF=PDF/max(PDF); %nor
y=ylim;
PDF=PDF*y(2);
hold on
plot(x,PDF,'r-','LineWidth',2);
hold off
These two blocks give me two different Gaussians as shown in the first picture. I do not understand why they fit data so badly: is it because of the tail on the RHS?
In the second plot I need to apply Gaussian fit to the last peak only. How should I do it?
And finally, after applying the fit, fit results are outputed onto the screen. Is there a function to save them into an array or so for later use?
Thanks in advance!
Regarding your last question, see How can I access the fitted distribution parameters from the HISTFIT function in Statistiics Toolbox ?
There is a workaround for this isssue by making a copy of the HISTFIT
function and modifying the code to pass out the "pd" variable. One way
to do this is to change the "h" output on the first line of the code
to "varargout" and add the following to the end of the file:
h = [hh; hh1];
argout={h,pd};
if nargout > length(argout)
error('Too many output arguments.');
end
[varargout{1:nargout}]=argout{1:nargout};
If I have an RC circuit with transfer function 1/(1+sRC) how do I draw the transfer function using MATLAB?
Num2=[1];
Den2=[R*C 1];
RCcirc=tf(Num2,Den2);
How do I declare the R and the C so that there are no errors?
tf is the wrong tool for plotting the transfer function. Try these instead:
Use linspace to generate a range of values for s. Give R and C reasonable values of your choice.
Read up on arithmetic operations in MATLAB, especially ./
Look at how to use plot and familiarize yourself with the command using some simple examples from the docs.
With these you should be able to plot the transfer function in MATLAB :)
First of all you need to understand what transfer function you want. Without defined values of R and C you won't get any transfer function. Compare it to this, you want to plot a sine wave: x = sin(w*t), I hope you can agree with me that you cannot plot such a function (including axes) unless I specifically say e.g. t is the time, ranging from 0 seconds to 10 seconds and w is a pulsation of 1 rad/s. It's exactly the same with your RC network: without any values, it is impossible for numerical software such as MATLAB to come up with a plot.
If you fill in those values, you can use th tf function to display the transfer function in whatever way you like (e.g. a bode plot).
On the other hand, if you just want the expression 1/(1+s*R*C), take a look at the symbolic toolbox, you can do such things there. But to make a plot, you will still have to fill in the R and C value (and even a value for your Laplace variable in this case).