Matlab Loop Help for x = 1:15 | Troubleshooting and Plotting

  • Context: MATLAB 
  • Thread starter Thread starter j-lee00
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary
SUMMARY

The discussion focuses on troubleshooting a MATLAB loop that iterates from 1 to 15, where the user attempts to assign values to variable 'y' based on the value of 'x'. The primary issue identified is the incorrect use of conditional statements, specifically the lack of equal signs in the comparisons and the omission of boundary values 5, 10, and 15. The correct syntax should include explicit comparisons for these boundary values to ensure all intended cases are covered. Additionally, the plotting command is improperly placed, which may lead to errors in visualizing the results.

PREREQUISITES
  • Familiarity with MATLAB syntax and programming structure
  • Understanding of conditional statements in programming
  • Basic knowledge of plotting functions in MATLAB
  • Experience with variable assignment and scope in MATLAB
NEXT STEPS
  • Review MATLAB conditional statements and their correct syntax
  • Learn about MATLAB's logical operators for comparisons
  • Explore MATLAB plotting functions and their usage
  • Investigate debugging techniques in MATLAB for troubleshooting code
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and students learning programming concepts who need assistance with loops, conditional logic, and plotting in MATLAB.

j-lee00
Messages
93
Reaction score
0
for x = 1:15

if x < 5
y = 2*x - 5;

elseif 5 < x <10
y = 4*x;

elseif 10< x <15
y = x*x;

end;

end;

plot(x,y)

Why doesn't this work?
 
Physics news on Phys.org
no equal signs, you are leaving out 5, 10 and 15 right off the bat.
 

Similar threads

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