How do I use relational and logical operators to find specific function values?
3 次查看(过去 30 天)
显示 更早的评论
Hi, I'm suppose to determine a) the time when the function h(t) exceeds the value 15, b) the time when the function h(t) exceeds the value 15 while the function v(t) does not exceed 36. I have to use relational och logical operators to determine these values. Is there any way to do this without creating a vector for the values between 0 and t_hit since then I have to set my own interval and therefore won't get a very precise answer? Is there a better approach to this problem?
MATLAB code:
close all; clear all; clc;
A = pi/6; v0 = 40; g = 9.81;
t_hit = (2*(v0/g)*sin(A));
t = 0:0.001:t_hit;
h = @(t) v0*t*sin(A)-0.5*g*t.^2;
v = @(t) sqrt(v0.^2-2*v0*g*t*sin(A)+g.^2*t.^2);
H=h(0:0.001:t_hit);
V=v(0:0.001:t_hit);
z1 = (H>15);
z2 = (H>15 & V<36);
T1=[]; T2=[];
for i=1:4078
if z1(i)==1
T1=[T1, t(i)];
end
if z2(i)==1
T2=[T2, t(i)];
end
end
2 个评论
Juliano Souza dos Passos
2018-10-18
编辑:Juliano Souza dos Passos
2018-10-18
I'm not sure if I got your question. I'll try to answer based on points a and b. A) Time t for h > 15
coln = find(z1);
Time_Exceeds15 = t(coln(1)); %Time which h > 15 for the first time
For question B) Time interval for h > 15 and v < 36
T_interval = t(z2);
H_interval = H(z2);
V_interval = V(z2);
Please use your code, but you can exclude the structure 'for'.
采纳的回答
Luna
2018-10-18
You can do it without creating z1 and z2. But you can't do it without creating H and V. You have to create your functions outputs.
try below instead of for loop:
dt = 0.001; % Sample Time
time1 = find(H>15)*dt;
time2 = find((H>15 & V<36))*dt;
1 个评论
Guillaume
2018-10-18
Well, it could be done analytically without creating H and V, using fzero or similar, but since this homeworks specifically asks to use relational operators I would think that discretizing the functions is indeed what is expected.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!