"Z must be a matrix, not a scalar or vector." PLEASE HELP!
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kevin Zhou
am 27 Sep. 2020
Beantwortet: Rafael Hernandez-Walls
am 27 Sep. 2020
So this is the function I want graph using surf.m:
%simple multivariate function;
function [f]=ps2func(x1,x2)
if ((x1<=2) &&(x2<=2))
f=4.*x1+3.*x2; %case 1 for the function
elseif ((x1>2) &&(x2>2))
f=x1.^2+x2.^2; %case 2 for the function
else
f=0; %case 3 for the function
end
%this is the suf.m function i used in an attempt to graph it:
x1 = 1:.1:4;
x2 = 1:.1:4;
[X,Y] = meshgrid(x1,x2);
Z= [f];
mesh(X,Y,Z)
grid on
hold on
0 Kommentare
Akzeptierte Antwort
Matt J
am 27 Sep. 2020
Bearbeitet: Matt J
am 27 Sep. 2020
You should have something like this:
x1 = 1:.1:4;
x2 = 1:.1:4;
mesh(x1,x2, ps2func(x1,x2) );
grid on
hold on
function f=ps2func(x1,x2)
if isvector(x1), x1=x1(:);x2=x2(:).'; end
f1=4.*x1+3.*x2; region1=(x1<=2) & (x2<=2);
f2=x1.^2+x2.^2; region2=(x1>2) & (x2>2);
f=false(size(f2));
f(region1)=f1(region1);
f(region2)=f2(region2);
end
Weitere Antworten (1)
Rafael Hernandez-Walls
am 27 Sep. 2020
x1 = 1:.1:4;
x2 = 1:.1:4;
[X,Y] = meshgrid(x1,x2);
Z= ps2func(X,Y);
mesh(X,Y,Z)
grid on
hold on
function [f]=ps2func(x1,x2)
f=size(x1);
f=((x1<=2)&(x2<=2)).*(4.*x1+3.*x2)+((x1>2)&(x2>2)).*(x1.^2+x2.^2);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Interpolation 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!