Проблема в том, что string() функция может неправильно форматировать матрицы для вывода в текстовое поле. Давайте исправим вывод матрицы с помощью sprintf():
// Создание главного окна
fig = figure('position', [100 100 600 550], ...
'backgroundcolor', [0.9 0.9 0.9], ...
'tag', 'main_window');
// Фрейм для выбора задачи
frame_task = uicontrol(fig, 'style', 'frame', ...
'position', [20 380 560 150], ...
'backgroundcolor', [0.9 0.9 0.9]);
rb1 = uicontrol(frame_task, 'style', 'radiobutton', ...
'string', 'Задача 1: График функции', ...
'position', [20 100 300 30], ...
'callback', 'select_task(1)');
rb2 = uicontrol(frame_task, 'style', 'radiobutton', ...
'string', 'Задача 2: 3D график', ...
'position', [20 70 300 30], ...
'callback', 'select_task(2)');
rb3 = uicontrol(frame_task, 'style', 'radiobutton', ...
'string', 'Задача 3: Обратная матрица 4x4', ...
'position', [20 40 300 30], ...
'callback', 'select_task(3)');
// Фрейм для параметров
frame_params = uicontrol(fig, 'style', 'frame', ...
'position', [20 150 560 220], ...
'backgroundcolor', [0.9 0.9 0.9]);
// Задача 1
edit_N1 = uicontrol(frame_params, 'style', 'edit', ...
'position', [150 180 60 25], ...
'string', '15', ...
'visible', 'off');
text_N1 = uicontrol(frame_params, 'style', 'text', ...
'string', 'Количество точек N:', ...
'position', [20 180 120 25], ...
'visible', 'off');
rb_graph1 = uicontrol(frame_params, 'style', 'radiobutton', ...
'string', 'График', ...
'position', [20 140 100 25], ...
'visible', 'off');
rb_table1 = uicontrol(frame_params, 'style', 'radiobutton', ...
'string', 'Таблица', ...
'position', [130 140 100 25], ...
'visible', 'off');
// Задача 2
edit_N2 = uicontrol(frame_params, 'style', 'edit', ...
'position', [150 180 60 25], ...
'string', '100', ...
'visible', 'off');
edit_M2 = uicontrol(frame_params, 'style', 'edit', ...
'position', [150 140 60 25], ...
'string', '4', ...
'visible', 'off');
text_N2 = uicontrol(frame_params, 'style', 'text', ...
'string', 'Количество точек N:', ...
'position', [20 180 120 25], ...
'visible', 'off');
text_M2 = uicontrol(frame_params, 'style', 'text', ...
'string', 'Количество значений M:', ...
'position', [20 140 120 25], ...
'visible', 'off');
rb_graph2 = uicontrol(frame_params, 'style', 'radiobutton', ...
'string', 'График', ...
'position', [20 100 100 25], ...
'visible', 'off');
rb_table2 = uicontrol(frame_params, 'style', 'radiobutton', ...
'string', 'Таблица', ...
'position', [130 100 100 25], ...
'visible', 'off');
// Поля ввода матрицы 4x4
matrix_edits = [];
for i = 1:4
for j = 1:4
edit = uicontrol(frame_params, 'style', 'edit', ...
'position', [30 + (j-1)*60, 170 - (i-1)*30, 50, 25], ...
'string', '0', ...
'visible', 'off');
matrix_edits = [matrix_edits, edit];
end
end
text_matrix = uicontrol(frame_params, 'style', 'text', ...
'string', 'Введите матрицу 4x4:', ...
'position', [20 200 150 20], ...
'visible', 'off');
// Кнопка расчета
calc_button = uicontrol(fig, 'style', 'pushbutton', ...
'string', 'Рассчитать', ...
'position', [250 100 100 30], ...
'callback', 'calculate()');
global current_task
function select_task(task_num)
global current_task
current_task = task_num;
set(rb1, 'value', 0);
set(rb2, 'value', 0);
set(rb3, 'value', 0);
select task_num
case 1 then
set(rb1, 'value', 1);
set(edit_N1, 'visible', 'on');
set(text_N1, 'visible', 'on');
set(rb_graph1, 'visible', 'on');
set(rb_table1, 'visible', 'on');
set(edit_N2, 'visible', 'off');
set(edit_M2, 'visible', 'off');
set(text_N2, 'visible', 'off');
set(text_M2, 'visible', 'off');
set(rb_graph2, 'visible', 'off');
set(rb_table2, 'visible', 'off');
set(text_matrix, 'visible', 'off');
for i = 1:16
set(matrix_edits(i), 'visible', 'off');
end
case 2 then
set(rb2, 'value', 1);
set(edit_N1, 'visible', 'off');
set(text_N1, 'visible', 'off');
set(rb_graph1, 'visible', 'off');
set(rb_table1, 'visible', 'off');
set(edit_N2, 'visible', 'on');
set(edit_M2, 'visible', 'on');
set(text_N2, 'visible', 'on');
set(text_M2, 'visible', 'on');
set(rb_graph2, 'visible', 'on');
set(rb_table2, 'visible', 'on');
set(text_matrix, 'visible', 'off');
for i = 1:16
set(matrix_edits(i), 'visible', 'off');
end
case 3 then
set(rb3, 'value', 1);
set(edit_N1, 'visible', 'off');
set(text_N1, 'visible', 'off');
set(rb_graph1, 'visible', 'off');
set(rb_table1, 'visible', 'off');
set(edit_N2, 'visible', 'off');
set(edit_M2, 'visible', 'off');
set(text_N2, 'visible', 'off');
set(text_M2, 'visible', 'off');
set(rb_graph2, 'visible', 'off');
set(rb_table2, 'visible', 'off');
set(text_matrix, 'visible', 'on');
for i = 1:16
set(matrix_edits(i), 'visible', 'on');
end
end
endfunction
// Функция для форматирования матрицы в строку
function result = matrix_to_string(matrix)
[rows, cols] = size(matrix);
result = [];
for i = 1:rows
row_str = "";
for j = 1:cols
row_str = row_str + sprintf("%8.4f ", matrix(i,j));
end
result = [result; row_str];
end
endfunction
function calculate()
global current_task
select (current_task)
case 1 then
// Задача 1 (без изменений)
N = evstr(get(edit_N1, 'string'));
a = -%pi/2;
b = %pi/2;
x_values = linspace(a, b, N);
y_values = ((x_values.^2 + 1).^(1/3)) ./ (0.5 + sqrt(abs(x_values) + 0.5)) .* x_values ./ (1 + sin(x_values).^2);
if get(rb_graph1, 'value') == 1 then
f = figure();
plot(x_values, y_values, 'r-', 'LineWidth', 2);
xlabel('x', 'fontsize', 3);
ylabel('f(x)', 'fontsize', 3);
title('График функции f(x)', 'fontsize', 3);
xgrid();
legend(['f(x)'], 2);
else
msg = ["x f(x)"; "----------------"];
for i = 1:N
msg = [msg; sprintf("%8.4f %8.4f", x_values(i), y_values(i))];
end
messagebox(msg, "Таблица значений");
end
case 2 then
// Задача 2 (без изменений)
N = evstr(get(edit_N2, 'string'));
M = evstr(get(edit_M2, 'string'));
a = -2*%pi;
b = 2*%pi;
if get(rb_graph2, 'value') == 1 then
x_plot = linspace(a, b, N);
y_plot = linspace(a, b, N);
[X, Y] = ndgrid(x_plot, y_plot);
Z = 5*X.^2 - 2*Y.^2 + X.*Y;
f = figure();
plot3d(x_plot, y_plot, Z);
xlabel("x", "fontsize", 3);
ylabel("y", "fontsize", 3);
zlabel("f(x,y)", "fontsize", 3);
title("График функции f(x,y) = 5x² - 2y² + xy", "fontsize", 3);
colorbar();
else
x_values = linspace(a, b, M);
y_values = linspace(a, b, M);
msg = ["x y f(x,y)"; "----------------------------"];
for i = 1:M
x = x_values(i);
y = y_values(i);
f = 5*x^2 - 2*y^2 + x*y;
msg = [msg; sprintf("%7.4f %7.4f %9.4f", x, y, f)];
end
messagebox(msg, "Таблица значений");
end
case 3 then
// Задача 3: Ввод матрицы 4x4
m = zeros(4, 4);
for i = 1:4
for j = 1:4
idx = (i-1)*4 + j;
m(i, j) = evstr(get(matrix_edits(idx), 'string'));
end
end
// Проверка определителя
det_m = det(m);
if det_m == 0 then
messagebox("Матрица вырожденная (определитель = 0)! Обратной матрицы не существует.", "Ошибка", "error");
return;
end
// Находим обратную матрицу
m_inv = inv(m);
check = m * m_inv;
// Создаем окно с результатами
result_window = figure('position', [300 100 600 500]);
text_area = uicontrol(result_window, 'style', 'edit', ...
'position', [10 10 580 480], ...
'string', '', ...
'horizontalalignment', 'left', ...
'verticalalignment', 'top', ...
'fontsize', 3, ...
'fontname', 'Courier', ...
'max', 10, ...
'min', 1);
// ИСПРАВЛЕННЫЙ ВЫВОД МАТРИЦ
result_text = ["ИСХОДНАЯ МАТРИЦА A:"; ""];
result_text = [result_text; matrix_to_string(m)];
result_text = [result_text; ""; "ОБРАТНАЯ МАТРИЦА A^(-1):"; ""];
result_text = [result_text; matrix_to_string(m_inv)];
result_text = [result_text; ""; "ПРОВЕРКА (A * A^(-1)):"; ""];
result_text = [result_text; matrix_to_string(clean(check))];
result_text = [result_text; ""; sprintf("ОПРЕДЕЛИТЕЛЬ: %g", det_m)];
set(text_area, 'string', result_text);
end
endfunction
Что исправлено:
- Добавлена функция
matrix_to_string() — правильно форматирует матрицу в строки.
- Убрана функция
string() для матриц — она работала некорректно.
- Используется
sprintf() для форматирования каждого элемента матрицы.
- Добавлены заголовки для лучшей читаемости.
Теперь проверьте:
- Выберите Задача 3.
- Введите простую матрицу (например,
1 0 0 0; 0 2 0 0; 0 0 3 0; 0 0 0 4).
- Нажмите Рассчитать.
- В новом окне должны четко отображаться:
- Исходная матрица.
- Обратная матрица.
- Проверка (единичная матрица).
- Определитель.
Если всё видно и читается — отлично! Если по-прежнему пусто — попробуем другой подход (например, через messagebox вместо текстового поля).
Дайте знать результат!