Answered step by step
Verified Expert Solution
Question
1 Approved Answer
As we've seen, in Matlab we can do calculations by simply typing them: 2 + 2 If we run this code we get the answer,
As we've seen, in Matlab we can do calculations by simply typing them:
If we run this code we get the answer, which by default is called ans:
ans
If we want to specify a name for it we can do so:
myresult
if we run this we get:
myresult
If we put a semicolon at the end of the line the calculation is still performed but no output is given:
myresult ;
If we run this then there will be no output but myresult will still equal
For example we can take myresult and add :
myresult ;
myresult
This will yield:
ans
When we perform numerical calculations, by default Matlab shows us an approximate answer:
If we type this in and run it this gives us:
ans
Variable Names
We need to be careful about how we name variables. Matlab will happily let us overwrite its own commands and then if we try to use these commands later we might be in for a surprise. Consider the code:
sin ;
sin
The result of executing this code will be:
Index exceeds the number of array elements
'sin' appears to be both a function and a variable. If this is unintentional, use 'clear sin to remove the variable 'sin' from
the workspace.
Matlab is telling us that it's confused we assigned sin to be thereby trashing the internal sin sine command, but then we tried to use sin as a function. Matlab can tell something is up
An even more common mishap is that Matlab knows that the letter i represents the complex number If you just type i into the Code box:
i
And run it you'll get:
ans
i
However Matlab will happily let you overwrite this and forget it ever exists. The following line of code:
i
Will completely trash Matlab's knowledge of the complex number and any references to i from then on will use the number instead.
Getting Lots of Digits
We can get more digits by wrapping the answer in vpa, which stands for variable precision arithmetic. This gives us a lot more digits:
vpa
ans
If we want, say, digits of precision, we can ask for it:
vpa
Now we'll see, if we run this:
ans
Matlab has other ways to handle approximations but vpa is good for now. It'll show up here and there throughout the assignments so remember if something is wrapped in vpa it's because we want to see lots of digits of output.
Some Common Mathematical Expressions
Here are some common mathematical functions in Matlab. One or two might be unfamiliar, and that's okay, but think about what they all are. Each is listed followed immediately by its result:
Code:
cospi
Result:
ans
Code:
cosd
Result:
ans
Code:
sqrt
Result:
ans
Code:
log
Result:
ans
Code:
exp
Result:
ans
Code:
asinsqrt
Result:
ans
Code:
nthroot
Result:
ans
Code:
tanh
Result:
ans
Code:
abs
Result:
ans
Code:
factorial
Result:
ans
Code:
log
Result:
ans
Code:
log
Result:
ans
Errors
When you run or submit code pay close attention to any errors that might appear in red. While it's true that sometimes the errors that Matlab shows are confusing, more often than not they tell you exactly what the problem is
For example before doing the assignment try typing and running and submitting the following snippets of code, one by one. Take a few minutes to see what Matlab is telling you in its error message. Type them exactly as they are as each has an error.
First:
Second:
x
x
Third:
arcsin
Comments
A percentage symbol in Matlab indicates the beginning of a comment. Anything following a will be ignored so you can start lines with or put them at the end of lines. For example:
Let's make x
x
Now let's add
x x Here x has been changed and will be
Now Let's find the sine of pi
sinpi
Keey an eye out there will be comments here and there in the assignment descriptions so be aware that they're just comments helping to explain what's going on
Note: If you're copying my code for testing you don't need to copy the comments they're completely ignored by Matlab! As soon as it sees a it stops reading.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started