Matlab

Read Complete Research Material

MATLAB

Matlab Assignment



Matlab Assignment

Question (a)

Let's solve using Newton's Method. In Newton's method, we set f(x) = x2-2x take an initial guess (x0) for the solution and calculate f(x0). We then extrapolate (assuming f(x) is linear) to a new value of x which will make f(x) = 0.

i.e.

or

Figure 1. The solution to by Newton's method

The result will be the sum of the first 100 integers. All for ... end loops have the same format, it begins with for, followed by an index (i) and a range of numbers (1: n) (Bishop, 1997). Now let's do a program that does n steps (iterations) of Newton's method. We will need to input the function, its derivative, the initial guess, and the number of steps. The output will be the final value of x, i.e. xn. If we are only interested in the final approximation, not the intermediate steps, which is usually the case in the real world, then we can use a single variable x in the program and change it at each step.

Question (b) Matlab Function

For Loop

%script exercise.m

format bank % output with 2 dec places

invest = input('type initial investment: ')

r = 0.05; % rate of interest

bal = invest; % initial balance

year = 0; % initial year

disp(' Year Balance') % header for output

% (You can experiment with this)

while (bal < 2*invest) % repeat while balance is

% less than twice the investment,

% and stop when balance exceeds this

bal = bal + r*bal; % update bal

year = year + 1; % update year

disp([year,bal])

end

The if-elseif-else statement

A simple form of the if statement is

if (condition)

statements

end

Here condition and statements are the same as in the while loop, but in this case the statements are executed only once if condition is true and are not executed at all if condition is false. For example the following script divides 1 by i, provided i is non-zero; otherwise, j is not assigned a value.

While Loop

if (i ~= 0)

j=1/i;

end

The symbol ~= is a relational operator and stands for is not equal to. Other relational operators include ==,

<=, >=, etc. Type help ops to and out about these. Note the difference between the relational operator == and the usual use of the symbol =, which assigns a value to a variable.

The if-else statement allows us to choose between two courses of action. For example the following script reads in a number and prints out a message to say if it is negative or non-negative.

x = input(' Type x : ')

if (x<0)

disp('x is negative')

else

disp('x is non-negative')

end

Note that indenting of statements inside loops and if statements helps make your program more readable. Going further, adding elseif allows us to choose between a number of possible courses of action.

x = input(' Type x : ')

if (x<0)

disp('x is negative')

elseif (x>0)

disp('x is positive')

else

disp('x is zero')

end

A more general form is

if (condition1)

statementsA

elseif (condition2)

statementsB

elseif (condition3)

statementsC

...

else

statementsE

end

Question (c)

If we start with a guessed value, xi we can find a better guess by extrapolation.

Note that these derivatives are evaluated at the x = xi

Now we want our new vector xi+1 ...
Related Ads
  • Matlab / Numeric Analysis
    www.researchomatic.com...

    Matlab / Numeric Analysis, Matlab / Nu ...

  • Matlab Project
    www.researchomatic.com...

    Matlab Project, Matlab Project Assignm ...

  • Technology - Science
    www.researchomatic.com...

    With the Aerospace Toolbox software, we can bring mu ...

  • Game-Matlab
    www.researchomatic.com...

    25 Nov 2012 ... Game- Matlab , Game- M ...

  • Matlab Assignment
    www.researchomatic.com...

    Matlab Assignment, Matlab Assignment E ...