How to plot imported data into matlab equations
조회 수: 4 (최근 30일)
이전 댓글 표시
Kiaora MatLAB community
Below I have an equation defined as T. However, I do not know how to import the data into the equation and plot it.
syms omega mu epsilon sigma
k1 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) + 1);
k2 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) - 1);
k = k1 + 1i.*k2;
Z_0 = 377;
Z_b = sqrt(1/9.3) .* Z_0;
T = @(h) - (((4 .* exp(1i .* k .* h)) .* Z_0 .* Z_b)/((-Z_0)^2 + (exp(2i .* k .* h) .* (Z_0)^2) - 2 .* Z_0 .* Z_b - (2 .* exp(2i .* k .* h) .* Z_0 .* Z_b) - (Z_b)^2 + exp(2i .* k .* h) .* (Z_b)^2));
A = T(0);
B = abs(T./A);
theta = angle(T./A);
h = readtable('height.xlsx');
plot(h.Height(1:10,1),T)
I want to plot T against h. I used readtable but not sure if that is enough for the plot to show up.
Please help
댓글 수: 3
Walter Roberson
2022년 11월 6일
Aloe, you flagged this as a duplicate. What is the link to the other (earlier) version of the question that you think this is a duplicate of?
답변 (2개)
Walter Roberson
2022년 8월 1일
syms omega mu epsilon sigma
Symbolic variables.
k1 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) + 1);
k2 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) - 1);
So k1 and k2 are defined in terms of the symbolic variables epsilon, mu, omega, sigma
k = k1 + 1i.*k2;
and k is therefore defined in terms of them as well
T = @(h) - (((4 .* exp(1i .* k .* h)) .* Z_0 .* Z_b)/((-Z_0)^2 + (exp(2i .* k .* h) .* (Z_0)^2) - 2 .* Z_0 .* Z_b - (2 .* exp(2i .* k .* h) .* Z_0 .* Z_b) - (Z_b)^2 + exp(2i .* k .* h) .* (Z_b)^2));
T is defined as an anonymous function that invokves k and h and some numeric constants, so that means that T is an anonymous function involving symbolic variables epsilon, mu, omega, sigma and input variable h
A = T(0);
B = abs(T./A);
T(0) would evaluate function handle T passing in 0 for h. The result would normally be in terms of the symbolic variables epsilon, mu, omega, sigma but because you are multiplying k by h = 0, the symbolic variables all vanish, so A should be independent of the symbolic variables.
You then try to divide the handle to the anonymous function T by that value. You cannot divide a function handle by something. You could, though, do
B = @(h) abs(T(h)./A);
Then
theta = angle(T./A);
same problem, you cannot divide a function handle, but you could define
theta = @(h) angle(T(h)./A);
Then
plot(h.Height(1:10,1),T)
plot() does not accept the handle to an anonymous function. You could try
H = h.Height(1:10,1);
plot(H, T(H))
but... remember T involves the symbolic variables epsilon, mu, omega, sigma so evaluating T at a numeric location would return a symbolic expression in four symbolic variables.
An expression in four symbolic variables has four independent variables and one dependent result, and so requires a 5 dimensional plot in itself. But you have different h values, so really you have 5 independent variables and one dependent result, so you requires a 6 dimensional plot. It is very difficult to plot in 6 dimensions in a way that makes sense to humans, especially if you are doing a grid plot rather than a scattered plot.
댓글 수: 2
Walter Roberson
2022년 8월 1일
No!
You have
h = readtable('height.xlsx');
so your h variable is a table object. But you need to apply the function to some data stored in the table, h.Height(1:10) so it is most efficient to extract that data into a variable and use the variable.
If it would confuse you less, you could use
plot(h.Height(1:10,1), T(h.Height(1:10,1)))
Paul
2022년 8월 1일
One way that uses symbolic (which isn't really necessaray)
syms omega mu epsilon sigma h
k1 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) + 1);
k2 = omega .* sqrt((mu .* epsilon)/(2)) .* sqrt(sqrt(1 + ((sigma)/(epsilon .* omega))^2) - 1);
k = k1 + 1i.*k2;
Z_0 = 377;
Z_b = sqrt(1/9.3) .* Z_0;
T = - (((4 .* exp(1i .* k .* h)) .* Z_0 .* Z_b)/((-Z_0)^2 + (exp(2i .* k .* h) .* (Z_0)^2) - 2 .* Z_0 .* Z_b - (2 .* exp(2i .* k .* h) .* Z_0 .* Z_b) - (Z_b)^2 + exp(2i .* k .* h) .* (Z_b)^2));
T = matlabFunction(T)
T(1,[1; 2],1,1,1) % example
Now T is an anonymous function that can be evaluated for values of its input arguments. If that doesn't work, becaus of overflow or something, then the full symbolic approach would be
syms T
T(h,epsilon,mu,omega,sigma) = - (((4 .* exp(1i .* k .* h)) .* Z_0 .* Z_b)/((-Z_0)^2 + (exp(2i .* k .* h) .* (Z_0)^2) - 2 .* Z_0 .* Z_b - (2 .* exp(2i .* k .* h) .* Z_0 .* Z_b) - (Z_b)^2 + exp(2i .* k .* h) .* (Z_b)^2));
which can be evaluated for values of the inputs arguments
T([1;2],1,1,1,1)
vpa(ans)
댓글 수: 2
Paul
2022년 8월 1일
Please show the complete code. Inclulde any error messages that result from running the code.
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!