I don't understand the condition from FOR row 16 to row 26. Can explain each row form if row to the end please.

1 visualización (últimos 30 días)
syms x y z lambda
dist = x.^2+(y-1).^2+z.^2; % distance of an abitary point (x,y,z) from the point(0,1,0)
S = z.^2-(x.*y)-1 == 0; % surface constraint z^2 = xy+1 ------> z^2-xy-1 == 0
L = dist + lambda * lhs(S); % Langrange function
dL_dx = diff(L,x) == 0; % derivative of L with respect to x
dL_dy = diff(L,y) == 0; % derivative of L with respect to y
dL_dz = diff(L,z) == 0; % derivative of L with respect to z
dL_dlambda = diff(L,lambda) == 0; % derivative of L with respect to lambda
system = [dL_dx; dL_dy; dL_dz; dL_dlambda]; % build the system of equations
[x_val, y_val, z_val,lambda_val] = solve(system, [x y z lambda], 'Real', true); % solve the system of equations and display the results
results_numeric = double([x_val, y_val, z_val]); % show results in a vector of data type double
[rows columns]=size(results_numeric); % getting number of rows and columns
fprintf('points on the surface S that are closest to the point (0, 1, 0) are :\n');
fprintf(' x y z\n');
fprintf('--------------------------\n');
for i=1:rows
fprintf('(');
for j=1:columns
if j < columns
fprintf("%.4f , ",results_numeric(i,j));
else
fprintf("%.4f",results_numeric(i,j));
end
end
fprintf(")\n");
end
  1 comentario
Stephen23
Stephen23 el 20 de Mayo de 2022
The FOR loop and IF can be simplified, assuming that there is always one or more columns:
for ii = 1:rows
fprintf("(%.4f",results_numeric(ii,1));
fprintf(",%.4f",results_numeric(ii,2:end));
fprintf(")\n");
end

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 20 de Mayo de 2022
Lines 16 through 26:
for i=1:rows
fprintf('(');
for j=1:columns
if j < columns
fprintf("%.4f , ",results_numeric(i,j));
else
fprintf("%.4f",results_numeric(i,j));
end
end
fprintf(")\n");
end
are there to display results_numeric in the command window.
The idea is to print each row of results_numeric on its own line, enclosed in parentheses, with commas separating each number in the row.
% for each row:
for i=1:rows
% first print an open parenthesis, "(":
fprintf('(');
% then for each element of this row:
for j=1:columns
if j < columns
% if it's not the last element, print it with
% 4 decimal points of precision ("%.4f") followed by
% space-comma-space (" , "):
fprintf("%.4f , ",results_numeric(i,j));
else
% if it is the last element of the row, just print the number
% (without the space-comma-space afterwards):
fprintf("%.4f",results_numeric(i,j));
end
end
% now print the close parenthesis ")"
% and a newline character ("\n") so that whatever
% is printed next will begin on the next line:
fprintf(")\n");
end
  2 comentarios
Ngo Ha Gia Bao
Ngo Ha Gia Bao el 20 de Mayo de 2022
I am still not clear about how does if work like j and i work to caculate the clostest point to the surface . Can you explain more clearly about this please .Thank you very much
Voss
Voss el 20 de Mayo de 2022
j and i iterate over the columns and rows of results_numeric, which is the solution to the system of equations set up in the previous lines.
In other words, the system is set up first, then solve solves it, then the results are displayed in the command window using fprintf iterating over each row and column of results_numeric.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Performance and Memory 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!

Translated by