# 2d plot of a function with two variables

조회 수: 5 (최근 30일)
Vinci 2017년 5월 9일
답변: Star Strider 2017년 5월 9일
I'm trying to represent a waveform on a transmission line given by the following formula:
y(x,t)=A*sin(phase_constant*x-2*pi*freq*t)
I need to plot the amplitude y versus distance x along the line at time (t) = 0
Do I need a for loop to do this?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Stephen23 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
"Do I need a for loop to do this?"
No, you do not need to use a loop. Use ndgrid, or repmat, or bsxfun, or whatever suits your data, and make sure that you write vectorized code:
Also keep in mind the difference between array and matrix operations:

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

### 답변 (2개)

Star Strider 2017년 5월 9일
Since ‘t=0’ (or any other constant), this becomes a univariate function. You can plot it with fplot (or ezplot):
A = 42; % Substitute Correct Value
phase_constant = 2*pi*rand; % Substitute Correct Value
freq = 60; % Substitute Correct Value
y = @(x,t) A*sin(phase_constant*x-2*pi*freq*t);
figure(1)
fplot(@(x) y(x,0), [0 10])
grid
See the documentation on the various functions, and on Anonymous Functions for details.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

Santhana Raj 2017년 5월 9일
No.You need not. generate vectors of x and t. Then implement the function for y. Use mesh to plot result.
Remember to use the operator '.*' instead of * for element by element multiplication of a matrix/array/vector.

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

### 카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by