2d plot of a function with two variables

13 ビュー (過去 30 日間)
Vinci
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 件のコメント
Stephen23
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
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.

Santhana Raj
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 Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by