Plotting a Quarter Circle in MATLAB

  • Thread starter Thread starter jhnreid86
  • Start date Start date
  • Tags Tags
    Matlab Plot
Click For Summary
SUMMARY

The discussion focuses on plotting a quarter circle in MATLAB, specifically addressing an error encountered when attempting to access an index of a vector. The user defined a vector x with values from 0 to 1 but mistakenly used a zero-based index in the for loop. The solution involves correcting the loop to start from 1, as MATLAB indexing begins at 1, not 0. The corrected code should iterate from 1 to the length of x, ensuring proper access to the vector elements.

PREREQUISITES
  • Understanding of MATLAB programming syntax
  • Familiarity with vector operations in MATLAB
  • Knowledge of mathematical functions such as square root
  • Basic concepts of plotting in MATLAB
NEXT STEPS
  • Review MATLAB indexing rules to avoid common errors
  • Learn about MATLAB's plot function and its parameters
  • Explore vectorization techniques in MATLAB for efficient calculations
  • Investigate how to plot parametric equations in MATLAB
USEFUL FOR

Students, educators, and professionals working with MATLAB who need to visualize mathematical functions and troubleshoot common programming errors.

jhnreid86
Messages
1
Reaction score
0

Homework Statement



I am trying to plot simply a quarter of a circle but am having difficulty with it.




Homework Equations





The Attempt at a Solution



So far I have


x = 0:0.01:1;

for i = 0:length(x)

y = sqrt(1 - x(i)^2);

plot(x,y,'b-')

end


Basically what I am trying to do here is create a vector x with values from 0 to 1, and then plot each corresponding y coordinate.

I keep getting the following error

? Attempted to access x(0); index must be a positive integer or logical.

Error in ==> Untitled5 at 5
y = sqrt(1 - x(i)^2);


Any help would be greatly appreciated!
 
Physics news on Phys.org
you are trying to access the zeroeth index, MATLAB indices start with 1 and not 0.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K