How to plot the quadratic function surface?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Seyyed Mohammad Saeed Damadi
am 17 Feb. 2019
Beantwortet: Naman Bhaia
am 27 Feb. 2019
I want to plot
when
. I wrote the following codes and was wondering if there is a more efficient way with less number of lines:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/204447/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/204448/image.png)
clc
clear
Q = [0.7750, 0.3897; 0.3897 0.3250];
b = [-2,-1];
[X,Y] = meshgrid(-5:0.1:1);
x = X(:);
y = Y(:);
sx = size(X);
n = length(x);
z = zeros(n,1);
for i = 1:n
z(i) = 0.5*[x(i,1),y(i,1)]*Q*[x(i,1);y(i,1)] - b*[x(i,1);y(i,1)];
end
X = reshape(x,sx);
Y = reshape(y,sx);
Z = reshape(z,sx);
surf(X,Y,Z)
0 Kommentare
Akzeptierte Antwort
Naman Bhaia
am 27 Feb. 2019
Hey Seyyed,
I was able to simplify the code by using vectorized operations instead of for loop
clc
clear
Q = [0.7750 0.3897; 0.3897 0.3250];
b = [-2 -1];
[X,Y] = meshgrid(-5:0.1:1);
x = X(:);
y = Y(:);
sx = size(X);
a=0.5*diag([x y]*Q*[x';y'])-(b*[x';y'])';
Z = reshape(a,sx);
surf(X,Y,Z)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!