# Line Styles in a Graph in MATLAB

• MATLAB
• Drakkith
In summary: Thanks for the help.In summary, the code used to plot a periodic function on the same graph in MATLAB is:k = 1;w0 = sqrt(k);z = 0;%loop to plot a periodic function for 5 different values of r, the damping coefficient.for r = 0:0.5:2 w1 = sqrt(w0^2-(r^2/4)); for t = 0:0.1:10 z=z+1; %Performs the calculation for each data point.
Drakkith
Mentor
I'm trying to plot 5 periodic functions on the same graph in MATLAB and I can't seem to figure out how to do it correctly. The following code will work, except that I still need to change the line style for each line so that they are easy to tell apart.

Matlab:
k = 1;     %spring constant
w0 = sqrt(k);
z = 0;     %Used to count the elements of y, which is used in the for loops below.

%loop to plot a periodic function for 5 different values of r, the damping coefficient.
for r = 0:0.5:2
w1 = sqrt(w0^2-(r^2/4));
for t = 0:0.1:10
z=z+1;
%Performs the calculation for each data point.
y(z) = exp(-r*t/2)*sin(w1*t);

end
%Performs the plot of each function for each iteration of the outer loop.
plot(y);

z=0;
end

According to my book, the command used to plot with a different line style is: plot(x, y, '--')
However, any time I try to plot the above function using a command similar to plot(x, y), I just get a blank plot.

I've also tried using:

Matlab:
t = 0:0.1:10;
plot(t, exp(-r*t/2)*sin(w1*t));
But this just gives me an error, saying that the inner matrices must match or something.

Any ideas?

Drakkith said:
I'm trying to plot 5 periodic functions on the same graph in MATLAB and I can't seem to figure out how to do it correctly. The following code will work, except that I still need to change the line style for each line so that they are easy to tell apart.

Have you tried plotting one, using hold on; and plotting the next?

Drakkith said:
Matlab:
t = 0:0.1:10;
plot(t, exp(-r*t/2)*sin(w1*t));
But this just gives me an error, saying that the inner matrices must match or something.

Any ideas?

It's giving you an error because the sizes don't match. Try specifying the t element you want for the particular plot. Or changing r/w1 to match sizes.

Last edited by a moderator:
Thanks Student100.
I managed to figure it out right after you posted, lol.
Turns out I needed to use the matrix multiplication operator '.*' instead of just '*' when multiplying sin() by exp().
Once I changed that my plot(x, y) started working correctly and I could add in the logic to set the line style.

Matlab:
k = 1;  %spring constant
w0 = sqrt(k);

%Calculations and Printout
hold;

%loop to plot a periodic function for 5 different values of r
for r = 0:0.5:2
w1 = sqrt(w0^2-(r^2/4));
t = 0:0.1:10
%Performs the calculation for each data point
y = exp(-r*t/2).*sin(w1*t);
if (r == 0.5)
plot(t, y, '--');
end
if (r == 1)
plot(t, y, ':');
end
if (r==1.5)
plot(t, y, '-.');
end
if (r == 2|| r==0)
plot(t, y);
end
end
legend('r=0', 'r=0.5', 'r=1', 'r=1.5', 'r=2');
xlabel('Seconds');
ylabel('Amplitude');
title('Amplitude vs Time');
hold;

Drakkith said:
Thanks Student100.
I managed to figure it out right after you posted, lol.
Turns out I needed to use the matrix multiplication operator '.*' instead of just '*' when multiplying sin() by exp().
Once I changed that my plot(x, y) started working correctly and I could add in the logic to set the line style.

Matlab:
k = 1;  %spring constant
w0 = sqrt(k);

%Calculations and Printout
hold;

%loop to plot a periodic function for 5 different values of r
for r = 0:0.5:2
w1 = sqrt(w0^2-(r^2/4));
t = 0:0.1:10
%Performs the calculation for each data point
y = exp(-r*t/2).*sin(w1*t);
if (r == 0.5)
plot(t, y, '--');
end
if (r == 1)
plot(t, y, ':');
end
if (r==1.5)
plot(t, y, '-.');
end
if (r == 2|| r==0)
plot(t, y);
end
end
legend('r=0', 'r=0.5', 'r=1', 'r=1.5', 'r=2');
xlabel('Seconds');
ylabel('Amplitude');
title('Amplitude vs Time');
hold;

Cool glad you figured it out.

Was trying to get it to run in my version, but was having some problems. Finally just got it working so I could see what it was trying to do. New code is working pretty flawlessly here.

Drakkith

## 1. What are the default line styles in MATLAB?

The default line style in MATLAB is a solid line. However, there are other built-in line styles such as dashed, dotted, and dash-dot.

## 2. How can I change the line style in a graph?

To change the line style in a graph, you can use the "LineStyle" property in the "plot" function. For example, "plot(x,y,'LineStyle','--')" will create a dashed line.

## 3. Can I create my own custom line style in MATLAB?

Yes, you can create your own custom line style in MATLAB by using the "LineSpec" property in the "plot" function. This allows you to specify the line style, color, and marker for your graph.

## 4. How can I make a line thicker or thinner in MATLAB?

To adjust the thickness of a line in MATLAB, you can use the "LineWidth" property in the "plot" function. This property takes a numerical value, with larger numbers creating thicker lines.

## 5. Can I use different line styles for multiple lines in one graph?

Yes, you can use different line styles for multiple lines in one graph by specifying the "LineStyle" property for each line in the "plot" function. For example, "plot(x1,y1,'LineStyle','--',x2,y2,'LineStyle',':')" will create a dashed line for the first set of data and a dotted line for the second set.

Replies
1
Views
1K
Replies
8
Views
647
Replies
4
Views
2K
Replies
1
Views
571
Replies
5
Views
2K
Replies
2
Views
888
Replies
2
Views
3K
Replies
1
Views
924
Replies
1
Views
4K
Replies
11
Views
2K