Write a function named solveSystem that has three inputs: %two vectors % consist
ID: 642587 • Letter: W
Question
Write a function named solveSystem that
has three inputs: %two vectors
% consisting of the coefficients [a b c] of two line equations of the form ax + by = c and a vector of x values
% a. The function should output a vector giving the x and y values of the point of intersection between the two lines. If the lines are parallel, return the empty vector.
% b. Your function should also plot the two lines using the inputted vector of x values as x. In addition, on the same graph, plot the intersection point of the two lines. Make the first line blue, the second line red, and the intersection point a magenta diamond.
% Make sure that you label your plot appropriately
solveSystem(rand(1,3)*2-1, rand(1,3)*2-1, [-5 5])
end
function V = solveSystem(v1, v2, x)
% to solve for the intersection of lines a1x + b1y + c1 = 0
% [a1 b1 * x = -c1
% a2 b2] y -c2
use matlab to write the script
Explanation / Answer
All matlab functions and scripts are plain text files that contain matlab commands. Matlab will treat any file that ends in .m as either a function or a script. It can find .m files you've written that are in your ~/matlab directory, in the directory you have cd'd into from the matlab prompt, or in a directory you've started matlab with (ie,
starts up matlab and adds that directory to the places matlab will look for .m files in.)
Scripts
A script is just a list of commands to be run in some order. Placing these commands in a file that ends in .m allows you to "run" the script by typing its name at the command line. You type the name of the script without the .m at the end.
Functions
A function is capable of taking particular variables (called arguments) and doing something specific to "return" some particular type of result. A function needs to start with the line
function return-values = functionname(arguments)
so that matlab will recognize it as a function. Each function needs to have its own file, and the file has to have the same name as the function. If the first line of the function is
then the file must be named myfun.m. The function has arg1 and arg2 to work with inside the function (plus anything else you want to define inside it, and possibly some global variables as well), and by the end of the function, anything that is supposed to be returned should have a value assigned to it. This particular function is just one line long, and it returns answer, which is defined in terms of the two arguments arg1 and arg2.
Some useful tools for functions and scripts
You can write functions that can accept different numbers of arguments and decide what to do based on whether it gets two arguments or three arguments, for example.
For example, if I have to plot 20 similar data files for trials and I want to load each file and use the filename in the title, I can write a function that takes the filename as a string as an argument. To load it in the function, I can use
str = ['load ' filename]
to put together a command string, and
eval(str)
to run that command.
Then to use the filename in the title, I can use
str = ['title(' filename ')']
eval(str)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.