plotting a function with mesh

2 visualizaciones (últimos 30 días)
Artur Walter
Artur Walter el 17 de Oct. de 2019
Respondida: Swastik Sanjay Chaudhury el 4 de Dic. de 2019
Hallo
I want to plot a function with mesh and find it's minimum
clear; close; clc;
f = @(x)(x(1).^2 + x(2).^2);
N = 200;
x1 = 0:2/(N-1):2;
x2 = -2:4/(N-1):2;
[x1, x2]= meshgrid(x1, x2);
x3 = f(x1, x2);
mesh (x1, x2, x3)
z = fminsearch(g , [0.5, 0.5])
% or I tried
function z = f(x1, x2)
z = x1.^2 + x2.^2;
end
function z = g(x)
z = x(1).^2 + x(2).^2;
end
Everthing failed!
How can I do it?
  2 comentarios
Adam Danz
Adam Danz el 17 de Oct. de 2019
"Everthing failed!"
This doesn't tell us what the problem is. If you're getting an error message, provide the entire copy-pasted message. If you're not getting an error but the plot isn't as expected, provide more detail about that.
Adam Danz
Adam Danz el 17 de Oct. de 2019
For starters, you've got a local function named f and an anonymous function named f. You can't have both in the same script.

Iniciar sesión para comentar.

Respuestas (1)

Swastik Sanjay Chaudhury
Swastik Sanjay Chaudhury el 4 de Dic. de 2019
Hello Artur, I agree with the comment as mentioned by Adam Danz. A basic modification maked the code work without any problem.
clear; close; clc;
t = @(x)(x(1).^2 + x(2).^2);
N = 200;
x1 = 0:2/(N-1):2;
x2 = -2:4/(N-1):2;
[x1, x2]= meshgrid(x1, x2);
x3 = f(x1, x2);
mesh (x1, x2, x3)
z = fminsearch(t , [0.5, 0.5])
% or I tried
function z = f(x1, x2)
z = x1.^2 + x2.^2;
end

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by