To display the positive values of y as x goes from -10 to 10 using a while loop in MATLAB, you can use the following code:
main.m272 chars9 lines
This code initializes the value of x as -10, and then enters the while loop. Inside the loop, it calculates the value of y using the equation y = x^2 - 49
, and checks if y is positive using the if statement. If y is positive, it displays the value of y using the disp
function. Then, it increments the value of x by 1, and the loop continues until x reaches 10.
Please note that this code only displays the positive values of y. If you also want to plot the values, you can modify the code as follows:
main.m265 chars6 lines
In this modified version, we create an array of x values using -10:0.1:10
to get a more continuous plot. Then, we calculate the corresponding array of y values using x.^2 - 49
to apply the equation element-wise. Finally, we use the plot
function to plot the values of x and y, and add labels to the x and y axes using xlabel
and ylabel
functions respectively.
gistlibby LogSnag