Artemisia
Artemisia

Reputation: 133

Simple MatLab plot

I am a beginner in MatLab and I have to plot (0.5, 0.5), (-0.5, 0.5), (-0.5, -0.5) and (0.5, -0.5) with lines joining the point on the screen. Here is the code I have typed out so far:


function []=strain_rate_tensor(t)
axis([-1 1 -1 1])
hold on
plot(0.5, -0.5, 'b')
hold on
plot(-0.5, -0.5, 'b')
hold on
plot(0.5, 0.5, 'b')
hold on
plot(-0.5, 0.5, 'b')
hold on

I get a blank screen when I run this script. What is wrong with my plot? The code is to plot a fluid parcel and then apply some transformations on it.

Upvotes: 0

Views: 187

Answers (3)

lakshmen
lakshmen

Reputation: 29064

x1=0.5;
x2=-0.5;
y1=0.5;
y2=-0.5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 3);
hold on;
xlim([-1, 1]);
ylim([-1, 1]);

You can change the xlim and ylim accordingly.

Upvotes: 4

Benoit_11
Benoit_11

Reputation: 13945

You have a bit too many hold on there :)

Also you only plot points, here is a simple way to draw connecting lines:

clear all
clc

hold all

plot(0.5, -0.5, '*b')

plot(-0.5, -0.5, '*b')

plot(0.5, 0.5, '*b')

plot(-0.5, 0.5, '*b')


line([-0.5 0.5], [-0.5 -0.5],'Color','k','LineWidth',2)
line([0.5 0.5], [-0.5 0.5],'Color','k','LineWidth',2)
line([-0.5 0.5], [0.5 0.5],'Color','k','LineWidth',2)
line([-0.5 -0.5], [-0.5 0.5],'Color','k','LineWidth',2)

hold off

axis([-1 1 -1 1])

Giving this:

enter image description here

Upvotes: 1

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

You just plotted four dots, you probably either want to plot one or two lines, or four stars or bigger dots.

Make sure to check doc plot for examples on how to plot.

I think you want this:

plot([0.5 -0.5; -0.5 -0.5; 0.5 0.5; -0.5 0.5]','bo-')

Sidenote: You only need to use hold on once (before the first or second plot) and you may want to turn it off again after you finish.

Upvotes: 1

Related Questions