MATLAB: Plot vectors from a function

In summary: Resultant Vector')You are attempting to return the values by typing "help plot". You can also read about the plot function by typing "help plot" and clicking on the link that appears. Lastly, if you are having trouble plotting, you can try saving the results to your workspace and re-running the script.
  • #1
Feodalherren
605
6

Homework Statement


So I have a function that allows me to input two vectors and it will calculate the resultant vector. The code works fine.
I need to call on this function and plot the two input vectors and the resultant; it's easier to explain if you look at the code.

Homework Equations

The Attempt at a Solution


-------CODE-------
run ResVecfigure
grid off

%Data from ResVec
x1 = [ 0 v1_x ];
y1 = [ 0 v1_y ];
x2 = [ 0 v2_x ];
y2 = [ 0 v2_y ];
x3 = [ 0 vR_x ];
y3 = [ 0 vR_y ];
hold on
plot(x1,y1,x2,y2,x3,y3)
------------END--------------

But MATLAB first of all doesn't pull out v1_x, v1_y etc. out of the function. I want it to pull the values out of the function ResVec and store them in the workspace. Second of all, it seems to be refusing to plot these vectors and keeps plotting just one of them.
 
Physics news on Phys.org
  • #2
Why do you need to save the results to workspace? The idea behind functions is to be independent and simply return the results without bothering too much about the internal happenings.

Your plot command should plot all of your results, but it is impossible to trouble shoot without seeing the rest of your program. Did you try writing out the vectors to check that they are what you expect them to be?
 
  • #3
Ok I kept working on it and this is what I got:
-------------------------
run ResVec

grid off

%Data from ResVec
x1 = [ 0 v1_x ];
y1 = [ 0 v1_y ];
x2 = [ 0 v2_x ];
y2 = [ 0 v2_y ];
x3 = [ 0 vR_x ];
y3 = [ 0 vR_y ];
hold on
plot(x1,y1,'g')
plot(x2,y2)
plot(x3,y3,'r')
legend('vec1', 'vec2', 'Resultant Vector')
-----------------------
Now it plots all of the vectors but it still doesn't plot them unless I have v1_x etc. in the work space. That's why I want them there. It just says v1_x not defined if I don't run the function file before I run the plotting program.The assignment wants us to run ResVec as a function and then plot the vectors from that function:

"The script file should call (use) the function ResVec from Part 1 to compute the resultant vector."

Also while we're at it, how do I make the plot auto-scale nicely?
 
  • #4
So how are you attempting to return the values?
 
  • #5
if you type in "help plot" you will get a lot of info on the plot function. It will also link you to related functions.

that being said, the 'axis' function will allow you to set the axis
 
  • #6
If ResVec generates V1_ex, then you MUST allways run it before plotting, unless it already exists in your workspace
 

1. How do I plot a vector from a function in MATLAB?

To plot a vector from a function in MATLAB, you can use the "plot" function followed by the name of the function and the range of values you want to plot. For example, if your function is named "f" and you want to plot it from x=0 to x=10, you would use the command "plot(f, [0, 10])".

2. Can I plot multiple vectors from different functions on the same graph in MATLAB?

Yes, you can plot multiple vectors from different functions on the same graph in MATLAB by using the "hold on" command before each "plot" function. This will allow you to add multiple plots to the same figure.

3. How can I customize the appearance of my plot in MATLAB?

To customize the appearance of your plot in MATLAB, you can use various commands such as "xlabel" and "ylabel" to add labels to the axes, "title" to add a title, and "legend" to add a legend. You can also change the color, line style, and marker style of your plot using the "color", "linestyle", and "marker" options in the "plot" function.

4. Is it possible to save my plot as an image file in MATLAB?

Yes, you can save your plot as an image file in MATLAB by using the "saveas" function. This function allows you to specify the file format (such as .png or .jpg) and the name of the file you want to save your plot as.

5. How can I plot a 3D vector in MATLAB?

You can plot a 3D vector in MATLAB by using the "plot3" function. This function works similarly to the "plot" function, but allows you to specify a third variable for the z-axis. You can also customize the appearance of your 3D plot using commands such as "xlabel", "ylabel", and "zlabel" for the axes and "title" for the title of the plot.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Classical Physics
Replies
4
Views
894
  • Engineering and Comp Sci Homework Help
Replies
7
Views
891
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
832
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top