Please help with MATLAB code.
Thank you.
Just like when youre plotting y = f (x) you need to do two things – define f and define x (x is usually defined using linspace) Defining f: You can do this as an anonymous function. Start with something simple: f (s, t) = cos (s) * sin (t) Defining s and t: Were going to do this in two steps. First, were going to define the values s and t go over: s = linspace (0, 4*pi, 200);t = linspace (0, 2*pi, 100); The naive thing to try is just evaluating f (s, t) and plotting it z = f (s, t);surf (s, t, z); What happened? What error message did you get? What are the matrix dimensions of s, t, and z? Now try plot3 (s, t, z) What did that plot? Why? The problem is that we need a grid of values to pass to f, not just two arrays. This is where the meshgrid function comes in: [S, T] = meshgrid (s, t);Z = f (S, T) What are the matrix dimensions of S and T? Z? Now try plotting
Expert Answer
s = linspace(0,4*pi,200/(4*pi));
t = linspace(0,2*pi,100/(2*pi));
size(s) %row:1 col:15
size(t) %row:1 col:15
z = f(s,t);
size(z) %row:1 col:15
% surf(s,t,z) => %error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)
plot(s,t,z)
[S,T] = meshgrid(s,t);
Z = f(S,T);
plot(S,T,Z)
Here you go champ. Hope this helps you. Let me know if you need anything else.