• Support PF! Buy your school textbooks, materials and every day products Here!

MATLAB Help - Comparing values/Indexing

  • Thread starter nvalia
  • Start date
  • #1
4
0

Homework Statement



I am very, very new to MATLAB, and can't seem to figure out this seemingly simple problem.

Given a t-array and a y-array for a decaying exponential function, my task is to find the first relative maximum in the graph without using Matlab's preset relative max. functions. I'm attempting to do this by comparing y-values at a certain "t" to y-values at "t0" and "t1" (before and after "t"). I'm using indexing, for loops, etc.


Homework Equations



N/A


The Attempt at a Solution



function[max,t_max] = relMax(t,y)
for i = 1:.001:length(t);
y(:,i+1) = y;
y0 = y(:,i);
y1 = y(:,i+2);
if (y0<y && y>y1)
max = y;
disp(max)
t_max = t;
disp(t)
break
end
end

---

Clearly I am making some kind of stupid mistake here. I don't really know how to use indexing very well, and it's causing me problems.

Thanks in advance!
 
Last edited:

Answers and Replies

  • #2
CEL
656
0

Homework Statement



I am very, very new to MATLAB, and can't seem to figure out this seemingly simple problem.

Given a t-array and a y-array for a decaying exponential function, my task is to find the first relative maximum in the graph without using Matlab's preset relative max. functions. I'm attempting to do this by comparing y-values at a certain "t" to y-values at "t0" and "t1" (before and after "t"). I'm using indexing, for loops, etc.


Homework Equations



N/A


The Attempt at a Solution



function[max,t_max] = relMax(t,y)
for i = 1:.001:length(t);
y(:,i+1) = y;
y0 = y(:,i);
y1 = y(:,i+2);
if (y0<y && y>y1)
max = y;
disp(max)
t_max = t;
disp(t)
break
end
end

---

Clearly I am making some kind of stupid mistake here. I don't really know how to use indexing very well, and it's causing me problems.

Thanks in advance!
Indexes must be integers. You should make your loop as
for i = 1: length(t)

0.001 is the increment in t, not in the index.
 
  • #3
4
0
Thank you! I figured out what I was doing wrong. :)
 

Related Threads on MATLAB Help - Comparing values/Indexing

  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
1
Views
1K
Replies
1
Views
1K
Replies
6
Views
3K
Replies
0
Views
1K
  • Last Post
Replies
1
Views
970
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
715
  • Last Post
Replies
2
Views
7K
  • Last Post
Replies
0
Views
3K
Top