问题描述
我想写一个程序,在一个半圆的顶部绘制一定间隔的点,并在其他地方绘制直线。像这样: __ n __
。
我定义了一个时间域,它被存储为一个向量( t = 0:0.01:5
)。我假设我可以使用时间向量的元素定义半圆顶点:
if t> = 2 | t y = sqrt(.15 ^ 2-(t-2.15)^ 2);
但是MATLAB产生了一个错误信息,说只有平方矩阵可以平方。
我试图利用指数来表明我想平方t向量的一个元素,而不是整个向量:
i = [200:230]; $ t $ b(t)(200:230)
y = sqrt(.15 ^ 2-(t(i)-2.15)^ 2);
在这些失败之后,我注意到用一列非零元素来平方形矩阵会产生一个新的方阵与第一个矩阵元素的列平方。如果有一种方法可以在矩阵平方后消除多余的零列,我可以使用矩阵的性质来平方t向量的值。
什么是解决这个问题的最简单和最有效的方法是什么?
听起来好像你想绘制一个半圆形的水平线碰撞就可以了。你可以这样做:
t = 0:0.01:5; %创建时间向量
y = zeros(size(t)); %创建与t
index = find((t> = 2)&(t y(index)= sqrt(.15 ^ 2-(t(索引)-2.15)。 %将bump添加到y
y(1:index(1))= y(index(1)); %在bump之前添加行
y(index(end):end)= y(index(end)); %在bump之后添加行
在上面的解决方案中,碰撞可能略高于或低于另一个(取决于您的样品在 t
下降的位置)。如果你想确保它们处于同一高度,你可以做以下操作:
index =(t> = 2)& (t y(index)= sqrt(.15 ^ 2-(t(索引)-2.15)。 %添加bump到y
%选项#1:$ b $ y(〜index)= y(find(index,1,'first')); %使用第一个圆点作为高度
%OPTION#2:
y(〜index)= y(find(index,1,'last')); %使用最后一个圆点作为高度
最后,您可以画线:
plot(t,y);
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?
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);
这篇关于MATLAB分段函数+矢量操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!