How can I plot maximum values using a for loop in MATLAB?

Click For Summary
SUMMARY

The discussion focuses on plotting maximum values using a for loop in MATLAB. The user aims to replace a static number with a variable defined as a range, specifically using the variable 'n' to compute 'r' and subsequently 't'. The correct implementation involves element-wise operations, specifically using '.*' for multiplication and './' for division to avoid division by zero errors. The final goal is to plot 'n' against the maximum values of 't' for each corresponding 'x'.

PREREQUISITES
  • Familiarity with MATLAB syntax and operations
  • Understanding of element-wise operations in MATLAB
  • Basic knowledge of plotting functions in MATLAB
  • Concept of maximum value extraction in arrays
NEXT STEPS
  • Learn about MATLAB element-wise operations and their syntax
  • Research how to use the 'max' function in MATLAB to extract maximum values
  • Explore MATLAB plotting functions, specifically 'plot' and its parameters
  • Investigate handling division by zero in MATLAB to avoid runtime errors
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those involved in data visualization, numerical analysis, and anyone looking to enhance their skills in plotting functions with variable parameters.

physicsCU
Messages
198
Reaction score
1
I wish to use a for loop i already made and replace a number with a variable defined as a range of numbers, then plot the max of another function that used the function with the new variable.

How would I do this?

example:

x = [0:.5:1]
r = 5*x
t = 3/r

new program:

x = same
n = [0:.25:.5]
r = n*x
t = 3/r

for each value of n, i want to plot the cooresponding t, and plot n vs t
 
Last edited:
Physics news on Phys.org
I do not understand what it is you want to do. However your new program does not work. Try
x = [0:.5:1]
n = [0:.25:.5]
r = n.*x
t = 3./r

One of your r's will of course be zero, so you have a small problem with one t. if you want to plot it
plot(t,n)
 
yes, i know about mtimes

it was simply an example. i guess i want to graph the max t at each x.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K