![]() |
Problem Solving with ComputersUniversity of New Brunswick - FrederictonFaculty of Computer Science |
Frequently Asked Questions - updated:2017-08-15
- Q6 Why do some commands end with a semi-colon (Tue Aug 15, 2017)?
- In many programming languages, the semi-colon is needed at the end of EVERY command statement to indicate that it is the end of the statement. However, matlab does NOT use the semi-colon for this purpose. It recognizes the end of a line as the end of a command statement. Instead of end-of-line, matlab interprets the semi-colon as a 'supress-display' indicator for assignment statements. If you include it at the end of an assignment statement, the result of the command will execute, but will not display in the command window. If you don't include, the result will be displayed.
- Q5 How do I run mutliple iterations of the same commands in my script> (Tue Aug 15, 2017)
- Most programming languages, including matlab, have control commands which allow you to set up loops. Loops enclose commands which run over-and-over again until some criteria is met which tells the script to stop 'looping'; The following snippet of code is an example:
- stuff that comes before loop...
for h=1:10 %sets h=1 and runs the commands in the loop, then sets h=2 and repeats.
v=(1/3)(pi*R^2*h); %calculates the volume with the current values stored in h
end %closes the loop...stops repeating when h=10.- Q4 How do I plot graphs in matlab? (Mon Aug 14, 2017)?
- Matlab has a function called plot() which you can use to plot graphs. The following snippet exemplifies how to use this function:
- t = 0:1:5; %creates a 1x6 matrix with elements [0 1 2 3 4 5 6] and stores it in variable 't'
d=[1 2 4 8 16 32]; %creates a 1x6 matrix with elements [1 2 4 8 16 32] and stores it in variable 'd'
figure(1) %createst a figure to display the graph
plot(t,d) %plots the variable 't' vs the variable 'd' in the figure
xlabel('Time (sec)'); %labels the x-axis
ylabel(distance (m));%labels the y-axis
- Q3 How do I get users to input parameters in matlab? (Mon Aug 14, 2017)?
- Matlab has a function called input() which you can use to get users to input values for your program to use. The following snippet exemplfies how to use this function:
- value = input('what is the value? ');
a = value + 1; %adds 1 to the value input by the user- Q2 Do I have to submit my Lab Exercise (Mon Aug 14, 20175)?
- NO - you do these exercises to make sure you are keeping up-to-date with how to apply the coding components in lecture. You will eventually be assessed on this through tests and exams. If you are not certain you are doing an exercise correctly, bring what you have done to the lab and ask the instructor or TA for help. When you think you are finished a lab exercise correctly, show the instructor or TA to have them verify that you are on the right track. While you will not be graded on attending the labs or completing the lab exercises, statistics show that students who complete less than 4 labs under supervision generally fail the exam.
- Q1 Do I have to submit my Lecture Activity work (Mon Aug 14, 2017)?
- NO - you do these activties to make sure you are keeping up-to-date with the lecture material and that you are understanding it. You will eventually be assessed on it through tests and examinations. If you are not certain you are doing a question correctly, bring what you have done to the instructor during office hours, and ask specific questions about it. For instance, if you are not sure if you solutions are well-formed, show the instructor one of your solutions and ask if they think it is sufficiently well-formed. Or, if you are not sure how to generate some code, bring what you have generated so far, and explain where you are stuck and what you have tried.
- You can also ask questions during lectures to help clarify what you don't understand. It is reasonable to have questions about the lecture material - Instructors are not 'perfect' communicators, and inevitably material that is new to you will be presented. Of course, you can only ask questions if you have spent some time sussing out what you do and do not understand. Staying current with lecture material and asking for help when you don't understand something will help you during midterms and finals, because you will already know how to do most of what has been covered.