How to plot imported data into matlab equations

2 views (last 30 days)
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)
Error using plot
Invalid data argument.
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 Comments
Aloe
Aloe on 1 Aug 2022
@Chunru What if I want them as constants? I was told to use syms if I want them as constants
Walter Roberson
Walter Roberson on 6 Nov 2022
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?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 1 Aug 2022
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 Comments
Aloe
Aloe on 1 Aug 2022
Edited: Aloe on 1 Aug 2022
@Walter Roberson If I were to use
plot(H, T(H))
then does that mean that I need to change all the h used in the code to H?
Walter Roberson
Walter Roberson on 1 Aug 2022
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)))

Sign in to comment.


Paul
Paul on 1 Aug 2022
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 = function_handle with value:
@(epsilon,h,mu,omega,sigma)(exp(-h.*(omega.*sqrt(sqrt(1.0./epsilon.^2.*1.0./omega.^2.*sigma.^2+1.0)-1.0).*sqrt((epsilon.*mu)./2.0)-omega.*sqrt(sqrt(1.0./epsilon.^2.*1.0./omega.^2.*sigma.^2+1.0)+1.0).*sqrt((epsilon.*mu)./2.0).*1i)).*(-1.864237404282749e+5))./(exp(-h.*(omega.*sqrt(sqrt(1.0./epsilon.^2.*1.0./omega.^2.*sigma.^2+1.0)-1.0).*sqrt((epsilon.*mu)./2.0).*2.0-omega.*sqrt(sqrt(1.0./epsilon.^2.*1.0./omega.^2.*sigma.^2+1.0)+1.0).*sqrt((epsilon.*mu)./2.0).*2.0i)).*6.419981795790556e+4+3.363444161381954e+4)
T(1,[1; 2],1,1,1) % example
ans =
-4.1023 - 1.0533i 1.8974 - 1.3832i
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)
ans = 
vpa(ans)
ans = 
  2 Comments
Aloe
Aloe on 1 Aug 2022
@Paul I tried inputting my data to the varible h using this but not getting anything
Paul
Paul on 1 Aug 2022
Please show the complete code. Inclulde any error messages that result from running the code.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!