Good day,
I'm hawing a issue with 3D, I hawe to make that "Z would be a matrix, not a scalar or vector". Pleas give mea a hint how to acheve it.
x=0:100:50000;
y=x;
inc=(x+y)/2;
br_lim=[9408 14532 57051 270500];
for v1=1:length(inc)
y(v1)=(inc(v1)-br_lim(1,1))/10000;
g(v1)=(inc(v1)-br_lim(1,2))/10000;
if inc(v1)<br_lim(1,1)
Z(v1)=0;
elseif inc(v1)>=br_lim(1,1) && inc(v1)<br_lim(1,2)
Z(v1)=(972.87.*y(v1)+1400).*y(v1);
elseif inc(v1)>=br_lim(1,2) && inc(v1)<br_lim(1,3)
Z(v1)=(212.02.*g(v1)+2397).*g(v1)+972.79;
elseif inc(v1)>=br_lim(1,3) && inc(v1)<br_lim(1,4)
Z(v1)=.42*inc(v1)-8963.74;
else inc(v1)>=br_lim(1,4);
Z(v1)=.45*inc(v1)-17078.74;
end
end
[X,Y]=meshgrid(x,y);
figure
surf(X,Y,Z)

 채택된 답변

Rik
Rik 2020년 8월 5일

1 개 추천

You are only filling a vector of Z, not the entire plane. Make sure you have a Z value for every value of X and Y. If you post (a description of) the function you're trying to implement, we can help you make a vectorized solution. Something like the code below.
[X,Y]=meshgrid(0:100:50000);
Z=zeros(size(X));
inc=(X+Y)/2;
y=(inc-br_lim(1,1))/10000;
g=(inc-br_lim(1,2))/10000;
br_lim=[9408 14532 57051 270500];
L= inc<br_lim(1) ;
Z(L)=0;
L= inc>=br_lim(1) & inc<br_lim(2) ;
Z(L)=___
%etc

댓글 수: 3

Andrew
Andrew 2020년 8월 5일
Thank You for sharing with me Your approach. It works fine.
I’m just starting to explore MATLAB syntax. I would like to confirm some points of Your syntax, do I understand them correctly. If You can spear some time…
  1. I was trying to start by creating two equal vectors/arrays (x,y) and before plotting to make a meshgrid from them. While You made meshgrid (square) by entering only the length of a side, and in [] you named grid’s sides – X and Y. Is it possible to create an uneven meshgrid (rectangular)?
  2. Elimination of for loop was achieved by the creation of size/index L, which is changing trough lines of code. So it is an index or some size? While I with loop for was creating a vector/array v and conditioning it with if/else statements. Seams to be that approach with L is spearing not only som script lines.
Rik
Rik 2020년 8월 5일
  1. Yes. You can read the documentation for meshgrid and ndgrid for all available options.
  2. L is a logical array (hence the name). It has the same size as Z. You will have to produce an output with the number of elements equal to the number of true elements in L. I would suggest experimenting with a small array to make sure you understand it.
[X,Y]=meshgrid(linspace(0,2*pi,100));
Z=zeros(size(X));
L= sin(X)>0 | cos(Y)>0 ;
Z(L)=sin(X(L))+sin(Y(L));
surf(X,Y,Z)
Andrew
Andrew 2020년 8월 5일
Thank You once more.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2020년 8월 4일

댓글:

2020년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by