How to fix the error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
% Set random seed value
rng(123);
% Generate random x and y arrays
x = rand(1, 250) * 10 - 5;
y = rand(1, 250) * 10 - 5;
% Plot circles
theta = linspace(0, 2*pi, 100);
bullseye_x = cos(theta);
bullseye_y = sin(theta);
middle_x = 2 * cos(theta);
middle_y = 2 * sin(theta);
outer_x = 3 * cos(theta);
outer_y = 3 * sin(theta);
figure;
plot(bullseye_x, bullseye_y, 'k-', 'LineWidth', 1.5); hold on;
plot(middle_x, middle_y, 'k-', 'LineWidth', 1.5);
plot(outer_x, outer_y, 'k-', 'LineWidth', 1.5);
% Define the function to calculate regions
function [bullseye, middleCircle, outerCircle, miss] = mytargetpractice(x, y)
% Calculate distances from the center
distances = sqrt(x.^2 + y.^2);
% Define regions using logical arrays
bullseye = distances <= 1;
middleCircle = distances > 1 & distances <= 2;
outerCircle = distances > 2 & distances <= 3;
miss = distances > 3;
end
% Calculate the number of points in each region
[bullseye, middleCircle, outerCircle, miss] = mytargetpractice(x, y);
p02a = sum(bullseye);
p02b = sum(middleCircle);
p02c = sum(outerCircle);
p02d = sum(miss);
% Plot the points in each region
plot(x(bullseye), y(bullseye), 'ro', 'MarkerEdgeColor', 'k');
plot(x(middleCircle), y(middleCircle), 'go', 'MarkerEdgeColor', 'k');
plot(x(outerCircle), y(outerCircle), 'bo', 'MarkerEdgeColor', 'k');
plot(x(miss), y(miss), 'yo', 'MarkerEdgeColor', 'k');
% Add labels, lines, title, legend, and save the figure
title('Target Practice Plot - YourUsername@calpoly.edu');
xlabel('X-axis');
ylabel('Y-axis');
xline(0, 'k--');
yline(0, 'k--');
legend('Bullseye', 'Middle Circle', 'Outer Circle', 'Miss', 'Location', 'best');
axis equal;
xticks(-5:1:5);
yticks(-5:1:5);
box on;
% Save the figure as .fig and .png
savefig('p02fig.fig');
print('p02fig.png', '-dpng');
Error: File: lab07.m Line: 67 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "myquadrants" function definition to before the first local function definition.
0 comentarios
Respuestas (1)
Star Strider
el 23 de Feb. de 2024
The error message is clear.
Put the function at the end of the file and the code works —
% Set random seed value
rng(123);
% Generate random x and y arrays
x = rand(1, 250) * 10 - 5;
y = rand(1, 250) * 10 - 5;
% Plot circles
theta = linspace(0, 2*pi, 100);
bullseye_x = cos(theta);
bullseye_y = sin(theta);
middle_x = 2 * cos(theta);
middle_y = 2 * sin(theta);
outer_x = 3 * cos(theta);
outer_y = 3 * sin(theta);
figure;
plot(bullseye_x, bullseye_y, 'k-', 'LineWidth', 1.5); hold on;
plot(middle_x, middle_y, 'k-', 'LineWidth', 1.5);
plot(outer_x, outer_y, 'k-', 'LineWidth', 1.5);
% Calculate the number of points in each region
[bullseye, middleCircle, outerCircle, miss] = mytargetpractice(x, y);
p02a = sum(bullseye);
p02b = sum(middleCircle);
p02c = sum(outerCircle);
p02d = sum(miss);
% Plot the points in each region
plot(x(bullseye), y(bullseye), 'ro', 'MarkerEdgeColor', 'k');
plot(x(middleCircle), y(middleCircle), 'go', 'MarkerEdgeColor', 'k');
plot(x(outerCircle), y(outerCircle), 'bo', 'MarkerEdgeColor', 'k');
plot(x(miss), y(miss), 'yo', 'MarkerEdgeColor', 'k');
% Add labels, lines, title, legend, and save the figure
title('Target Practice Plot - YourUsername@calpoly.edu');
xlabel('X-axis');
ylabel('Y-axis');
xline(0, 'k--');
yline(0, 'k--');
legend('Bullseye', 'Middle Circle', 'Outer Circle', 'Miss', 'Location', 'best');
axis equal;
xticks(-5:1:5);
yticks(-5:1:5);
box on;
% Save the figure as .fig and .png
savefig('p02fig.fig');
print('p02fig.png', '-dpng');
% Define the function to calculate regions
function [bullseye, middleCircle, outerCircle, miss] = mytargetpractice(x, y)
% Calculate distances from the center
distances = sqrt(x.^2 + y.^2);
% Define regions using logical arrays
bullseye = distances <= 1;
middleCircle = distances > 1 & distances <= 2;
outerCircle = distances > 2 & distances <= 3;
miss = distances > 3;
end
.
0 comentarios
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!