Answered step by step
Verified Expert Solution
Link Copied!

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:
2+2
If we run this code we get the answer, which by default is called ans:
ans =
4
If we want to specify a name for it, we can do so:
myresult =2+2
if we run this we get:
myresult =
4
If we put a semicolon at the end of the line the calculation is still performed but no output is given:
myresult =2+2;
If we run this then there will be no output but myresult will still equal 4.
For example we can take myresult and add 3:
myresult =2+2;
myresult +3
This will yield:
ans =
7
When we perform numerical calculations, by default Matlab shows us an approximate answer:
1/7
If we type this in and run it, this gives us:
ans =
0.1429
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 =5;
sin(40)
The result of executing this code will be:
Index exceeds the number of array elements (1).
'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 5, 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 =
0.0000+1.0000i
However Matlab will happily let you overwrite this and forget it ever exists. The following line of code:
i =10
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(1/7)
ans =
0.14285714285714285714285714285714
If we want, say, 10 digits of precision, we can ask for it:
vpa(1/7,10)
Now we'll see, if we run this:
ans =
0.1428571429
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:
cos(pi/4)
Result:
ans =
0.7071
Code:
cosd(30)
Result:
ans =
0.8660
Code:
sqrt(5)
Result:
ans =
2.2361
Code:
log(6)
Result:
ans =
1.7918
Code:
exp(0.2)
Result:
ans =
1.2214
Code:
asin(sqrt(2)/2)
Result:
ans =
0.7854
Code:
nthroot(5,3)
Result:
ans =
1.7100
Code:
tanh(3)
Result:
ans =
0.9951
Code:
abs(-7.3)
Result:
ans =
7.3000
Code:
factorial(5)
Result:
ans =
120
Code:
log10(3)
Result:
ans =
0.4771
Code:
log2(3)
Result:
ans =
1.5850
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:
((3+2)/2+1
Second:
x=2
2x+3
Third:
arcsin(1/2)
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=2
x =2
% Now let's add 1.
x = x +1% Here x has been changed and will be 3.
% Now Let's find the sine of pi/7...
sin(pi/7)
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

How do parents and peers influence adolescentspg12

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago

Question

What is meant by Career Planning and development ?

Answered: 1 week ago

Question

What are Fringe Benefits ? List out some.

Answered: 1 week ago

Question

Develop skills for building positive relationships.

Answered: 1 week ago

Question

Describe techniques for resolving conflicts.

Answered: 1 week ago

Question

Give feedback effectively and receive it appropriately.

Answered: 1 week ago