MATLAB Help - Comparing values/Indexing

  • Thread starter Thread starter nvalia
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion centers on using MATLAB to find the first relative maximum of a decaying exponential function without utilizing built-in functions. The user attempts to implement this using a custom function named relMax, which employs indexing and for loops. Key mistakes identified include incorrect indexing and loop increments, specifically using a non-integer increment in the loop. The solution involves adjusting the loop to iterate over integer indices corresponding to the length of the input arrays.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with arrays and indexing in MATLAB
  • Knowledge of for loops in MATLAB
  • Concept of relative maxima in mathematical functions
NEXT STEPS
  • Review MATLAB array indexing techniques
  • Learn about MATLAB for loop syntax and best practices
  • Study how to identify relative maxima in datasets
  • Explore MATLAB's built-in functions for maximum value detection
USEFUL FOR

Beginner MATLAB users, students working on numerical analysis, and anyone interested in implementing custom algorithms for mathematical functions.

nvalia
Messages
4
Reaction score
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:
Physics news on Phys.org
nvalia said:

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.
 
Thank you! I figured out what I was doing wrong. :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K