Plot function line specification - matlab

I am trying to plot a graph using a straight line in MATLAB; however, I can only print it using dot circles.
I tried changing "ro-" with "r-" and other different solutions but nothing worked. When using "r-" it does not print anything.
This is my code:
for T = temp
figure(i)
for xb = linspace (0,1,10)
xt = 1-xb;
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot = Pb*xb + Pt*xt;
yb = (Pb*xb)/Ptot;
plot(xb, Ptot, 'bo-.')
hold on
plot(yb, Ptot, 'ro-')
end
i = i + 1;
saveas(gcf, filename, 'png')
end
This is what I get:
This is what I want:
How can I make this plot with lines?

To plot a line, the plot command must get all the points along the line in one function call. Repeated calls to plot will add new lines to the figure, and in this case each line is composed of a single point. In short, MATLAB doesn't know you want to connect these dots.
So how do you get all the data points in one array? Simply store them within your loop:
T = 70;
xb = linspace(0,1,10);
Plot = zeros(size(xb)); % preallocate output array
yb = zeros(size(xb)); % preallocate output array
for ii=1:numel(xb)
xt = 1-xb(ii);
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot(ii) = Pb*xb(ii) + Pt*xt;
yb(ii) = (Pb*xb(ii))/Ptot(ii);
end
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')
But it is actually easier to do this without a loop at all:
T = 70;
xb = linspace(0,1,10);
xt = 1-xb;
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot = Pb*xb + Pt*xt;
yb = (Pb*xb)./Ptot; % note ./ is the element-wise division
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')

Related

How do I use code to generate a graph where the line crosses the origin in Matlab?

I am trying to use Matlab to generate a line graph, but the line terminates at the last point, and doesn't go all the way to the origin. Is there any way to make it so that the line goes beyond the points in code?
I've attached the code that I'm currently using, along with pictures of what the graph looks like right now and how I want it to look.
%Enter Data
fnet = [0.465, 0.560, 0.670, 0.763, 0.870, 0.971, 1.063];
faccel = [0.434, 0.514, 0.612, 0.684, 0.776, 0.850, 0.915];
asys = [0.4963, 0.6034, 0.7074, 0.8088, 0.9210, 1.030, 1.138]
mh = [0.050, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11]
x = fnet;
y = asys;
%Model Equation
model = 'm*x'
%the model can be changed, ex. m*x^2
IV = 'x'
DV = 'y'
%Create and perform curve fit
newfit = fittype(model, 'Independent', IV, 'Dependent', DV);
%result and goodness of fit, prime symbol converys rows to columns
[result, gof] = fit(x', y', newfit, 'StartPoint', 1)
%plot fits and data points, create plot object for formatting
p = plot(result, x, y);
%style the data points
p(1).MarkerSize = 10;
p(1).Marker = '.';
p(1).MarkerFaceColor = 'blue';
%p(1).MarkerEdgeColor = 'green';
%style the line of best fit
p(2).LineWidth = 1;
p(2).Color = 'black';
%Create graph object, set formatting to latex
graph = gca;
set(graph, 'defaultTextInterpreter', 'latex');
set(legend, 'visible', 'off');
%format title and subtitle
graph.Title.String = {'System Acceleration vs. Net Force on System', 'in Modified Atwood Machine'};
graph.Title.FontSize = 16;
%subtitle, where we will place our equation and statistics
%specifically, the equation w/ units, r squared, slope with plusminus %
graph.Subtitle.Interpreter = 'latex';
graph.Subtitle.String = '$a_{sys} = 1.064 m^{-1}F_{net}, \, r^2=0.9994, m=1.064 \pm 0.007$';
graph.Subtitle.FontSize = 13;
%format x and y axes
graph.XLabel.Interpreter = 'latex';
graph.XLabel.String = '$F_{net} \: (N)$';
graph.XLabel.FontSize = 15;
graph.XLim = [0,1.5];
graph.XGrid = 'on';
graph.XMinorGrid = 'on';
graph.XMinorTick = 'on';
graph.YLabel.Interpreter = 'latex';
graph.YLabel.String = '$a_{sys} \: (\frac{m}{s^2})$';
graph.YLabel.FontSize = 15;
graph.YLim = [0,1.5];
graph.YGrid = 'on';
graph.YMinorGrid = 'on';
graph.YMinorTick = 'on';
Instead of using plot directly on the fit result object, you can call plot with a bit more control and evaluate the result directly for the line
Replace this:
p = plot(result, x, y);
with this:
hold on
p(1) = plot( x, y );
p(2) = plot( [0,2.2], feval(result, [0,2.2]) );
Note you could add the marker and line options during the plot calls now if you wanted, instead of updating p(1) and p(2) in retrospect.

Turning a plot3 into a surf in MATLAB

I have several csv files and I plotted them using plot3 to create the following image:
Now I would like to turn this into a surface plot because I would like to colour the plot according to height. I made the following with scatter3:
clearvars;
files = dir('*.csv');
name = '\epsilon_{y} over time for Vertical section';
des_col_1 = 'Vertical section.epsY []';
des_col_2 = 'Length [mm]';
set(gca,'FontSize',20)
a = gca;
ii = 1;
x_data = [];
y_data = [];
z_data = [];
tStart = tic;
for file = files'
csv = xlsread(file.name);
[n,s,r] = xlsread(file.name);
des_cols = {des_col_1,des_col_2};
colhdrs = s(1,:);
[~,ia] = intersect(colhdrs, des_cols);
colnrs = flipud(ia);
file.name = n(:,colnrs);
file.name = file.name(1:end,:);
x_data = [x_data; file.name(:,2)];
y_data = [y_data; ones(size(file.name(:,2))).*ii];
z_data = [z_data; file.name(:,1)];
ii = ii+1;
end
tEnd = toc(tStart);
fprintf('%d minutes and %f seconds\n',floor(tEnd/60),rem(tEnd,60));
view(40,40);
zlabel({'True strain (%)'});
xlabel({'Length along sample (mm)'});
ylabel({'Stage'});
title({name});
scatter3(a,x_data,y_data,z_data,10,z_data);
colormap(jet); %# or other colormap
which gives me this
That was made with a smaller set of data than the first one as a test. It does almost what I want but I was wondering if there was a way to generate a true 3D surface from all my data. I can create a matrix with x, y, and z values for all points and I tried to replace scatter3(a,x_data,y_data,z_data,10,z_data); with
[X,Y] = meshgrid(x_data,y_data);
f = scatteredInterpolant(x_data,y_data,z_data);
Z = f(X,Y);
surf(a,X,Y,Z);
but the plot that I get does not look very good
I'm pretty sure there's something wrong with the interpolation but I'm not very good with surfaces so I don't know how to correct it.
The reason surf is giving you the error is you are creating long nx1 arrays Where n = number of points per file times number of files. For x,y,& z_data and you need them into a matrix instead. So try the following changes:
for file = files'
<snipped out for length>
x_data = [x_data; file.name(:,2).'];
y_data = [y_data; ones(1,numel(file.name(:,2))).*ii];
z_data = [z_data; file.name(:,1).'];
ii = ii+1;
end
This should make x, y, and z_data the size nxm (n = number of files, m = number points per file).
Then you should be able to just do
surf(x_data,y_data,z_data)

How to find Orientation of axis of contour in matlab?

I want to find Orientation, MajorAxisLengthand MinorAxisLength of contour which is plotted with below code.
clear
[x1 , x2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [1,3];
sigm = [2,0;0,2];
xx_size = length(mu);
tem_matrix = ones(size(x1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {x1,x2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(x1));
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
% regionprops(z_plat,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');
In my opinion, I may have to use regionprops command but I don't know how to do this. I want to find direction of axis of contour and plot something like this
How can I do this task? Thanks very much for your help
Rather than trying to process the graphical output of contour, I would instead recommend using contourc to compute the ContourMatrix and then use the x/y points to estimate the major and minor axes lengths as well as the orientation (for this I used this file exchange submission)
That would look something like the following. Note that I have modified the inputs to contourc as the first two inputs should be the vector form and not the output of meshgrid.
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
% 0 0 0
major_length = [ellipsedata.long_axis];
% 4.7175 3.3380 2.1539
minor_length = [ellipsedata.short_axis];
% 4.7172 3.3378 2.1532
As you can see, the contours are actually basically circles and therefore the orientation is zero and the major and minor axis lengths are almost equal. The reason that they look like ellipses in your post is because your x and y axes are scaled differently. To fix this, you can call axis equal
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
axis equal
Thank you #Suever. It help me to do my idea.
I add some line to code:
clear
[X1 , X2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [-1,0];
a = [3,2;1,4];
a = a * a';
sigm = a;
xx_size = length(mu);
tem_matrix = ones(size(X1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {X1,X2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(X1));
figure;contour(X1, X2, z_plat,3, 'LineWidth', 2,'color','m');
hold on;
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
major_length = [ellipsedata.long_axis];
minor_length = [ellipsedata.short_axis];
tet = orientations(1);
x1 = mu(1);
y1 = mu(2);
a = sin(tet) * sqrt(major_length(1));
b = cos(tet) * sqrt(major_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);
tet = ( pi/2 + orientations(1) );
a = sin(tet) * sqrt(minor_length(1));
b = cos(tet) * sqrt(minor_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);

How to find and plot lower and upper limits of a line

In Matlab, how can one find and plot, e.g., the 10% lower and upper limits of a line. I would like easy to calculate and plot the 2 limit lines (blue; see example) given the red line.
stairs([repmat(1,[10 1]); repmat(3,[10 1]); repmat(2,[10 1])],'r'), hold on
stairs([repmat(1.1,[9 1]); repmat(3.1,[12 1]); repmat(2.1,[9 1])],'b--'),
stairs([repmat(.9,[11 1]); repmat(2.9,[8 1]); repmat(1.9,[11 1])],'b--'), hold off
Hope above make sens. Thank you in advance.
In this very specific case, a solution that works is given below.
Input is y and the two desired outputs are obviously up and dn.
fraction = .1;
jUp = find(diff(y)>0);
jDn = find(diff(y)<0);
up = y + fraction;
up(jUp) = up(jUp+1);
up(jDn+1) = up(jDn);
dn = y - fraction;
dn(jUp+1) = dn(jUp);
dn(jDn) = dn(jDn+1);
but there exist many generalisations of this problem for which this simple approach does not work.
A more advanced approach (including a delta in x and y direction, and fixing problems with overlap or exceeding bounds) would be:
y = [1*ones([10 1]); 3*ones([10 1]); 2*ones([10 1])];
yFrac = .8;
xFrac = 5;
% generate x values
x = 1:numel(y);
% find indexes of change
idx = find(diff(y))+1;
% consider only changing points
yNew = y([1;idx;end]);
xNew = x([1;idx;end]);
% find indexes of positive and negative change
pIdx = find(diff(yNew)>0)+1;
nIdx = find(diff(yNew)<0)+1;
% determine down-shift
yDn = yNew - yFrac;
xDn = xNew;
xDn(pIdx) = xDn(pIdx) + xFrac;
xDn(nIdx) = xDn(nIdx) - xFrac;
% fix dn-values that overlap
oIdx = find(diff(xDn)<0);
yDn(oIdx) = [];
xDn(oIdx+1) = [];
% determine up-shift
yUp = yNew + yFrac;
xUp = xNew;
xUp(pIdx) = xUp(pIdx) - xFrac;
xUp(nIdx) = xUp(nIdx) + xFrac;
% fix up-values that exceed bounds
epIdx = find(xUp>xNew(end));
yUp(epIdx+1) = [];
xUp(epIdx) = [];
enIdx = find(xUp<xNew(1));
yUp(enIdx-1) = [];
xUp(enIdx) = [];
% plot results
figure(1)
clf
hold on
stairs(xNew,yNew,'b')
stairs(xDn,yDn,'r--')
stairs(xUp,yUp,'r--')

Simple Phase Shift for Removing Instrument Response

So I have a simple program where I want to shift a simple sine function pi/2. Now I knwo this would be extremely easy by just inserting a bias (i.e. A*sin(2*pi*frequency + bias). But this program is a simple way to test a theory. I need to shift complicated magnetic data, but the shift is frequency dependent. So to figure out how to do that I just want to shift this sin wave by a set shift, but I want to do it in the frequency domain. While the code below doesn't show any errors, it does not shift the data properly and effects magnitude. Code is below. Thank you!
clear all
time = 1:0.01:2*pi; %Create time vector
mag = sin(2*pi*time);
Y = fft(mag); %transform
Pnewr = pi/2;
%t = angle(mag);
t = imag(Y);
%t_fin = t-Pnewr; %Subtract the pahse delay from the original phase vector
R=real(Y);
I=t;
k = I./R
Phi = tan(k);
PhiFinal = Phi-Pnewr;
PhiFinal = PhiFinal'
IFinal = R * atan(PhiFinal);
spec=complex(R,IFinal);
Finalspec = ifft(spec); %Invert the transform
Final = Finalspec;
plot(time,mag);
hold on
plot(time,Final,'r')
grid on
For one thing you are not recombining imaginary and real components properly, since
PhiFinal = PhiFinal'
IFinal = R * atan(PhiFinal);
is effectively a dot product, not an element by element product. In general it doesn't look like you are making correct use of the complex relationships. The following generates a clean phase shift:
time = 1:0.01:2*pi;
mag = sin(2*pi*time);
Y = fft(mag); %transform
Pnewr = pi/2;
R = real(Y);
I = imag(Y);
It = abs(Y);
% It = sqrt(I.^2 + R.^2);
Phi= angle(Y); % <-- using matlab function, equivalent to `Phi = atan2(I, R);`
k = I./R;
Phi0 = atan(k);
figure, subplot(121), plot(Phi0,Phi,'.'), xlabel('Phi from tan'), ylabel('Phi from ''angle'''), grid on, axis('tight')
PhiFinal = Phi-Pnewr; % <-- phase shift
IFinal = It .* sin(PhiFinal);
RFinal = It .* cos(PhiFinal);
spec= RFinal + 1i*IFinal;
Final = ifft(spec); %Invert the transform
subplot(122)
plot(time,mag);
hold on
plot(time,real(Final),'r')
plot(time,imag(Final),'r:')
grid on
axis('tight')
legend('Initial','Final'),xlabel('t'), ylabel('I')
mag*mag' % <-- check that total power is conserved
Final*Final'
These figures show the phase as computed with tan vs matlab's angle (which uses atan2), and the results of the phase shift (right panel):