given that y=x2- 49, write a while loop to display the positive values of y as x goes from -10 to 10 in matlab

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.m
x = -10;                 % Initial value of x
while x <= 10
    y = x^2 - 49;        % Calculate the value of y
    if y > 0             % Check if y is positive
        disp(y);         % Display the positive value of y
    end
    x = x + 1;           % Increment x
end
272 chars
9 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.m
x = -10:0.1:10;         % Create an array of x values
y = x.^2 - 49;          % Calculate the corresponding array of y values
plot(x, y);             % Plot the values of x and y
xlabel('x');            % Add x-axis label
ylabel('y');            % Add y-axis label
265 chars
6 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.

related categories

gistlibby LogSnag