MATLAB: Plot vectors from a function

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming problem related to plotting vectors generated from a function. Participants are addressing issues with retrieving vector values from the function and successfully plotting them, as well as concerns about workspace management and auto-scaling of plots.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a function that calculates a resultant vector from two input vectors but encounters issues with plotting due to undefined variables unless the function is run first.
  • Another participant questions the necessity of saving results to the workspace, suggesting that functions should return results independently.
  • A later reply indicates that the plot command can display all vectors but emphasizes the need to check the vectors' values to troubleshoot effectively.
  • One participant confirms that the function must be executed before plotting to ensure the required variables exist in the workspace.
  • Another participant suggests using the 'axis' function to manage plot scaling, although specifics on auto-scaling are not provided.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of storing function outputs in the workspace, with some advocating for independence of functions while others emphasize the need for accessible variables. The discussion remains unresolved regarding the best approach to manage workspace variables and plotting.

Contextual Notes

There are limitations regarding the assumptions about how the function ResVec operates and the specific requirements of the assignment, which are not fully detailed in the discussion.

Feodalherren
Messages
604
Reaction score
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
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?
 
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?
 
So how are you attempting to return the values?
 
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
 
If ResVec generates V1_ex, then you MUST allways run it before plotting, unless it already exists in your workspace
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K