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
编辑: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'.
Jakob
Jakob 2018-10-18
Thanks, works great for a and b! For c (which I did not mention b4) I have calculate the time when h < 5 or v > 35.
z3 = (H < 5 | V > 35);
This gives me two time intervals which are true for the statement:
coln3 = find(z3);
Time_int1 = t(coln3(1)); %Time which h < 5 or v > 35 for the first time.
Time_int2 = t(coln3(end)); %Time which h < 5 or v > 35 for the last time.
How do I find the times where the first interval ends and the second one starts?
The answer is: times between 0s and 1.5250s, as well as 2.5525s and 4.0775s

请先登录,再进行评论。

采纳的回答

Luna
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
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by