views:

986

answers:

2

I would like to write a program which plots the points on top of a semicircle on a certain interval and a straight line everywhere else. Something like this: __n__.

I defined a time domain, which was stored as a vector (t = 0:0.01:5). I assumed that I could define the points on the top of the semicircle using elements of the time vector:

if t>=2|t<=2.3
y = sqrt(.15^2-(t-2.15)^2);

but MATLAB produced an error message saying only square matrices can be squared.

I tried to utilize indices to show that I wanted to square an element of the t vector and not the whole vector:

i = [200:230];
for t(200:230)
y = sqrt(.15^2-(t(i)-2.15)^2);

After these failures, I noticed that squaring a square matrix with one column of non-zero elements would produce a new square matrix with a column of the first matrix's elements squared. If there is some way to eliminate the extra columns of zeros after squaring the matrix, I could use that property of matrices to square the values of the t vector.

What is the simplest and most effective way to address this problem?

A: 

Hold on, so your question is, you want to square each element of a vector? All you have to do is:

t.^2

The . represents an element-wise operation in MATLAB on a vector or an array.

And secondly, if I understood your problem currently, you want to create a vector y, which contains a function of elements of t such that t>=2 | t <=2.3?

If so, all you have to do is this:

y = sqrt(0.15^2-(t( (t>=2|t<=2.3) )-2.15).^2));

Essentially, I created a logical index (t>=2 | t<=2.3) and used to access only those elements (which I wanted) in t.

Also, I didn't fully understand what you wanted to achieve. Do you want to plot the topmost point (maxima) of a semicircular curve?

Jacob
The combination of these two answers has helped me a great deal. I wanted to plot a straight line which goes into the points of a semicircle and goes back into a straight line along with the response a car chassis would have to driving over a speed bump (modeled by the semicircle). Without your t.^2 explanation I would have assumed that the '.' in the other answer was a typo.THANK YOU BOTH!
CSCI GOIN KILL ME
Lol, you're welcome ... upvotes are welcome as well!
Jacob
+1  A: 

It sounds like you want to draw a horizontal line with a semicircular "bump" on it. Here's how you can do this:

t = 0:0.01:5;        % Create the time vector
y = zeros(size(t));  % Create a zero vector the same size as t
index = find((t >= 2) & (t <= 2.3));        % Find a set of indices into t
y(index) = sqrt(.15^2-(t(index)-2.15).^2);  % Add the "bump" to y
y(1:index(1)) = y(index(1));                % Add the line before the "bump"
y(index(end):end) = y(index(end));          % Add the line after the "bump"

In the above solution, the lines before and after the "bump" could be slightly higher or lower than one another (depending on where your samples in t fall). If you want to make sure they are at the same height, you can instead do the following:

index = (t >= 2) & (t <= 2.3);              % Find a set of logical indices
y(index) = sqrt(.15^2-(t(index)-2.15).^2);  % Add the "bump" to y
% OPTION #1:
y(~index) = y(find(index,1,'first'));  % Use the first circle point as the height
% OPTION #2:
y(~index) = y(find(index,1,'last'));   % Use the last circle point as the height

Finally, you can plot the line:

plot(t,y);
gnovice
This explanation has been helpful. The following is the code I compiled:`%DEFINE COMPONENTS OF TRANSFER FUNCTION:north = 1.31*10^6*[1 13.3];south = [1 516.1 5.685*10^4 1.307*10^6 1.733*10^7];h = tf(north,south);%TIME INTERVAL: t = 0:0.01:5; %DEFINE POINTS ON SPEED BUMP USING PIECEWISE FUNCTION:y = zeros(size(t)); index = find((t >= 2) y(index) = sqrt(.15^2-(t(index)-2.15).^2); y([1:index(1) index(end):end]) = y(index(1)); %PLOT PIECEWISE FUNCTION WITH RESPONSE:lsim(h,y,t);`THANK YOU!
CSCI GOIN KILL ME

related questions