Good morning.
I am trying to design a PD controller manually and i wanted to be able to have a plot with parameters on which i can change their values live and see their result on plot. I want to emphasize again that i want to do the design by manualy changing Pi and PD parameters and not automatically via matlab pid designer.(The question is more matlab affiliated).
Sorry for my bad English.
Thanks in advance
(My code follows):
clear all;
clc;
syms s;
K=1;
num = 4500*K;
den = sym2poly(s^2+361.7*s);
G=tf(num,den);
H=1;
%%
Kp=2;
Ki=0;
Kd=0;
C=pid(Kp,Ki,Kd,0);
T=feedback(C*G,H);
step(T);
You can make a simple matlab GUI for that, with textboxes or sliders to representing your gains and an axes object to hold your plot. Than in the callback functions of the textboxes/sliders you just create your system and controller based on the Value properties of the textbox/sliders, simulate the response and plot it in the axes object.
Here you can find some tutorials for matlab guis
Related
Are there any general guidelines on how Matlab handles graphics-based commands that ultimately result in no action being taken? A simple illustrative example--note the actual computational cost here is quite negligible:
fig=figure;
ax=axes;
for i=1:10
data=myFunction(i) %e.g. rand(i)
plot(data)
hold(ax,'on') %perform this repeatedly even though it's only needed once
end
versus:
fig=figure;
ax=axes;
for i=1:10
data=myFunction(i) %e.g. rand(i)
plot(data)
if ~ishold(ax)
hold(ax,'on') %perform this only if it is needed
end
end
If Matlab internally determines whether the hold(ax,'on') command is needed before actually doing it, then presumably the computational cost is similar or lower for the first form. The coding is also simpler to implement and read. But, if the action is carried out in full, then there are cases where it would be better, from a computational cost standpoint, to use the second form.
It's worth noting that the definition of "no action" here is deliberately vague, there's a lot of nuance here. For instance, it's easy to create an example where Matlab must perform some level of computation before it can evaluate whether the graphics command would have no effect. For instance, in colormap(myColormapFunction), Matlab would have to call myColormapFunction in order to evaluate whether what it returns is the same as the existing plot's CData property. Thanks.
As far as I know, there are no official guidelines on how "no action" commands are handled by the built-in MATLAB functions. Meanwhile, MathWorks does provide guidelines for optimizing graphics performance; which I feel, is a much more important thing to consider.
Graphics Performance Documentation Page
Now I apologize in advance if the following section doesn't answer your question. But if you are truly curious about the behind the scenes and real-world performance, you should use the provided Code Profiling Tool, and built-in timing functions.
With that said, the next section is about optimizing graphics performance in general.
For example, in the code you've provided, I would strongly recommend against putting hold and plot in the for-loop to begin with. From my experience, these are never necessary and can be optimized away.
Here, I'm guessing that you are trying to animate MATLAB plots; in which case, try updating the plot markers instead of using the plot function. For example:
figure
% Plot an empty plot with placeholder values (NaNs) the size of your data
% Save handle to plot object as `h`
h = plot( nan(size(data)) );
for i = 1:10
[Xdata, Ydata] = MyFunction(...);
% Update your plot markers with the handle update method
h.XData = Xdata;
h.YData = Ydata;
drawnow % drawnow to see animation
end
In this case, I don't even need to use the hold function, since I'm just updating the same plot, which is much faster.
Even if your function is outputting new data series, and you want to plot them on top of the old data series, you can use the same trick; it will only require you to pre-allocate the plot handles and data arrays. (Which, honestly is a good programming practice in general.)
figure
hold on % Hold the plot now
N = 100; % Number of data series you expect to have
H = cell(N,1); % Preallocate N Cells for all the plot handles
for i = 1:N
% Save plot handles to cell array in a loop, if you have so many series
H{i} = plot ( nan(size(data)) );
end
% Your iterative function calls
for t = 1:100
...
% Iteratively update all plot handles with a syntax like this
% (Not entirely sure, this is off the top of my head)
for i = 1:N
H{i}.XData = newX;
H{i}.YData = newY;
end
end
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
The default graphs produced from matlab are very different from what I see in books. For example the image below looks visually pleasing. Can I change default settings of matlab to mimic this graphing style?
This question will refrain from lecturing the OP on best practices for graphics and simply attempt to answer the question as asked. I personally concur with a few of the concerns raised but leave it to the OP to seek out resources on data visualization and graphical aesthetics. (For the record, I'm not a fan of the chart.)
Resources:
The MATLAB Plot Gallery depicts a range of plots and adjustments that may help you. For high quality, professional looking graphs, scroll down to the Advanced Plots to see source code and the resulting figures.
Graphical overview of the Types of MATLAB Plots available.
You can also make a basic plot then use MATLAB's Plot Editor to customize the properties through the graphical interface. When done, click File-->Generate Code and you'll see one possible way to code that graph. This is helpful when you know how to do something through the interface but want to script it in the future.
Examples with code for Publication Quality Plots with Matlab
Mathworks blog on Making Pretty Graphs
Another example on Creating high-quality graphics in MATLAB for papers and presentations
I realize some of these links may eventually expire. Please feel free to comment if they do
Example:
I'm no expert. I learned everything in this answer from looking at the documentation, plot source code, and playing with the properties for the various plot components.
% Functions of Interest % MATLAB 2018a
fh=#(x) a + a*sin(b*x) + 1-exp(-b*x);
gh=#(x) a + (a/b)*cos(c*x);
a = 20;
b = .3;
c = .2;
% Plot
X = (0:.01:25)';
figure, hold on
p(1) = plot(X,fh(X),'r-','DisplayName','Excitation')
p(2) = plot(X,gh(X),'b-','DisplayName','Recovery')
% legend('show') % Optional legend (omitted here since we're adding text)
xlabel('X')
ylabel('Y')
title('Particle Displacement')
% Options
ha = gca;
box on
grid on
ylim([-80 100])
set(ha,'GridLineStyle','--') % use ':' for dots
t(1) = text(3.5,80,'excitation')
t(2) = text(12,20,'recovery')
for k = 1:2
p(k).LineWidth = 2.2;
t(k).FontWeight = 'bold';
t(k).FontSize = 12;
t(k).FontName = 'Arial';
end
Create a function which takes a matrix of data where each row represents a signal that you want to plot.
Define some styles you want to use for plotting. In your example plot, the first two would be 'bo' and 'rx'. Just iterate over your rows and plot each row with a different style followed by the command "hold on;"
function fancyplot(xaxis, matrix)
figure;
style = {'bo', 'rx', 'k.'}; # and so on
for r = 1:size(matrix, 1)
plot(xaxis, matrix(r,:), style{r});
hold on;
end
end
Write another script which you execute right after you plot or add it to the function above. In this script use the the following methods to control the limits of the axis
xlim
ylim
Set them to the min/max values of the data you plotted.
To add text to your plots use the Text command.
If you want to use these plots in publications be mindful of the fact that most publications are black and white and your graphs should be distinguishable event if they are not colored (I doubt the ones above would be). I always believed doing all formatting in code is a good idea without the manual tinkering around. Otherwise you might figure out that you have to update all 8 plots in your publication at 4 am shortly before you need to submit your paper. If you run some simulations and all formatting is in code you can simply execute your formatting scripts and save the plots automatically how to save a picture from code, preferably to the eps format.
I am pretty new in matlab, and i am stuck in a problem that i have to solve before 29-12 !
I have the code that correctly solves a differential equation and plot the solution in a graph in which i have space vs time, now i am interested in what changes if i change the 2 initial parameters values but when i change them and solve again, the graph does not change at all, even if i setup big values of the parameters.
Here i show the code for the solver:
clear all;
close all;
clc;
T11=0;
dtode=0.001;
TNt=100;
R0=0.1;
init=R0;
[T,R] = ode23(#faili1,T11:dtode:TNt,init);
Ntode=lenght(T);
r=R(1:1:Ntode,1);
plot(T,r)
Ive saved a figure as a .fig-file in MATLAB which I did now reopen after some time.
Is there a way to access the data which is saved in the Histogram? I want to replot it by using the hist() command instead of imhist into a new figure (the reason is that matlab2tikz cant export the histogram plotted by imhist properly).
I imagine I could access the data when I would know the handle of the histogram, right?
EDIT:
A = findall(gcf,'type','axes');
then inspecting
get(A(i))
to see which axes the histogram is plotted in. This works but I have to figure out how to retrieve the actual data.
But I somehow assume that I have to look at a parent/children of the axes handle (depending which hierarchy MATLAB creates of objects).
Okay I figured it out finally.
As written in my edit above, you can use findall to find the handles of all axes-objects.
After using it, try to find out which handle refers to which axes by looking at the entries like X/YLim in get(A(i)), after finding the axes-ID and storing it (the k-th element in A) to idx = A(K), use this script to read the entries from the histogram plotted by imhist() -> The values are replicated as often as described by the bins (YData) and then replotted by hist into a new figure:
% ----------------------------------------------------------------------- %
b = get(idx);
b = get(b.Children); % Get the Plot-Handle
x = b.XData; % Bins
y = b.YData; % Bin-Counts
data = [];
for i = 1:length(x)
data = [data x(i)*ones(1,y(i))]; % replicate data
end
figure
hist(data, length(unique(x)));
xlim([min(data) max(data)]);
Edit: The for-loop is a quick and dirty one ;-) Im sure there's a nicer solution e.g. by using repmat, but I was only interested in a quick solution :-)
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.