An outstanding tutorial, clear and with valuable common/standard practice issues. Same for the other tutorials on your channel. A very good balance between capability and options. Thanks so much ....
how would i write a function rect = rect_aperture(r1, c1, r2, c2) that constructs a rectangular aperture on a 512 by 1024 grid? so the top left (r1,c1) and bottom right (r2,c2)?
You would start with (as you said): function rect = rect_aperture(r1,c1,r2,c2) I would also recommend making the dimensions (512 and 1024) as additional arguments, in order to make it more robust. If you want, you could make those variables have default values of 512 and 1024, if that's what makes sense in the context of your problem. Then, you would use the physics of your particular problem (which I don't know) in order to create the grid. I hope that helps!
You could potentially replace the for loop with a while loop (with careful variable management), but I don't think I see how to replace a conditional statement with a loop. Can you please elaborate on your idea?
Hi I was wondering if you have any expertise with the "comet" function and would be able to help me sort out some code? I have this code here: a=(1:0.5:1000); angle=a.*75; x=cos(angle); y=sin(angle); t=0.5; Ex=x.*Er; Ey=y.*Er; DELAY = 1; %edit comet %axis=5*10^12; comet(Ex,Ey,0) %axis([-axis axis -axis axis]); And it works except for the fact that it's too fast! I can't get it to slow down with any pause or delay functions, any ideas?
+Diamond Blooded I have actually never used the "comet" function, but after some exploration, I think I have a solution for you. My solution is to plot the figure over and over, but to emphasize the location of the current plotted point, as well as a trailing tail. (By the way, you didn't define "Er" in the code you posted, so I could not run it as is. I just made Er=1 to try it out.) You can control the length of the tail, as well as the delay between subsequent plots. Note, the code below will run for a long time, because I made the delay long to emphasize the operation. Here is my code, and please feel free to ask questions. a=(1:.5:1000); angle=a.*75; x=cos(angle); y=sin(angle); Er = 1; Ex=x.*Er; Ey=y.*Er; tail_len = 5; % tail length in number of points delay_time = 0.5; % delay time in seconds figure(1); hold on; plot(Ex(1:tail_len),Ey(1:tail_len),'r','LineWidth',4); plot(Ex(tail_len),Ey(tail_len),'ro','MarkerSize',12); pause(delay_time); hold off; for ii = tail_len:tail_len:length(Ex)-tail_len figure(1);plot(Ex(1:ii),Ey(1:ii),'b'); hold on; plot(Ex(ii:ii+tail_len),Ey(ii:ii+tail_len),'r','LineWidth',4); plot(Ex(ii+tail_len),Ey(ii+tail_len),'ro','MarkerSize',12); pause(delay_time); hold off; end figure(1);plot(Ex(1:ii),Ey(1:ii),'b'); hold on; plot(Ex(ii:end),Ey(ii:end),'r','LineWidth',4); plot(Ex(end),Ey(end),'ro','MarkerSize',12); hold off;
+Ilya Mikhelson Thankyou this gives a perfect representation of what i was looking for (Er was meant to be a few million but scaling doesn't matter). Although this would be perfect for what I have asked i need to be able to copy this multiple times and plot 9 plots on 1 graph, but thankyou so much for your help! Thankyou
+Diamond Blooded You can definitely make this a modular function. Then, you can use it over and over compactly. For example: function my_comet(x, y, tail_len, delay_time) % Insert my code with proper modifications to use the user input instead of hard-coding within the function.
+Rishabh Vyas The function would look something like this: function derivative = my_derivative(vector_1, vector_2) % Code The comment will be replaced with whatever you are doing with the input vectors. I hope this helps!
The problem with this is that a function needs to be called by its name, with potential input arguments. Therefore, it must be properly saved in the file path. If the thing you want is to have the user input various commands to execute, though, you can use something like this: eval(input('Enter a command: ','s')); This will prompt the user to input commands, save them as a string, and then evaluate the string.
Hey, How to write a function that its number of outputs depends on the value of its input...for example: [n1,n2,.........nr]=myfunction(r) is this possible in Matlab?
You can use the "varargout" output variable, which is a cell array you fill in inside the function. For example, "function [varargout] = myfunction(r)". In the function, you can use the value of "r" to determine how long to make the cell array "varargout". I hope this helps!
Here is an example: function [varargout] = myfunction(r) varargout = cell(1,r); for ii = 1:r varargout{ii} = ii*2; end You can now call it with up to "r" output variables, for example "[w,x,y,z] = myfunction(4)", in which case you will get w=2, x=4, y=6, z=8. You do have to be careful with error checking inside the function, though, since the user can accidentally request more outputs than you assign. You can do this with "nargout", which will tell you the number of requested output arguments. However, depending on your usage, it may be easier to simply have an output matrix or cell array, and to fill it in inside the function. Then, you will always call the function with one output argument (i.e. the matrix or cell array), instead of several (e.g. [w,x,y,z] = ...). I hope this makes more sense now. If not, please ask followup questions!