PDA

View Full Version : Matlab problem - Power Series implementation


skillz
25 Apr 2007, 11:48pm
Hi guys!

If anyone has any tips on starting this problem it will be a plus! i understand it, but im not so sure on implementing it in matlab.:confused:

This equation Implements the exponential power series, defined as:
y(x) = ex = 1 + x + (x^2)/2! + (x^3)/3! + (x^4)/4! + ...

Your function statement should be of the form:
function [x, y, err] = eseries(xs, xe, ns, nt)

The first and second arguments define the range of x over which the series is calculated, ns is the number of points at which the series is calculated, and nt is the number of terms (so nt = 3 are all terms up to and including the quadratic term).
The first output argument is the values of x at the which the series is calculated, and the second output
argument the values of the series. The third output argument should be the root-mean-square error between
your approximation to ex and the Matlab exp function defined over the same interval. An appropriate error
message should be printed in the command window if

• nargout < 3.
• nt < 3 or nt > 15.
• xs < 0.
• xs > xe.
• ns < 50.

I think i know how to implement the error stuff, its just getting started - not realy sure - please help!!!!

skillz
25 Apr 2007, 11:56pm
i heard that this can be done possible in as little as 5 lines of code!

shwaip
26 Apr 2007, 6:03pm
Yes, it is possible to write the code in a very small amount of lines...however, your assignment is for you to write this code. Why don't you give it a shot, and we can give feedback...

skillz
26 Apr 2007, 6:08pm
Yes, it is possible to write the code in a very small amount of lines...however, your assignment is for you to write this code. Why don't you give it a shot, and we can give feedback...

I have attempted it using loops - but the aim is to show that matlab can do it without loops and im not sure where to start this is how i done it with loops:


Let's say my X vector is

X = [0 1 2 3 4 5];

Then I loop so

Y(1) = 1 + 0 + 0^2/2! + 0^3/3! + .....
Y(2) = 1 + 1 + 1^2/2! + 1^3/3! + .....
Y(3) = 1 + 2 + 2^2/2! + 2^3/3! + .....
Y(3) = 1 + 3 + 3^2/2! + 3^3/3! + .....
Y(4) = 1 + 4 + 4^2/2! + 4^3/3! + .....
Y(5) = 1 + 5 + 5^2/2! + 5^3/3! + .....

and for each Y value I would loop for each term (since you don't know
this when you write the function).</pre>I just need some help getting started, pointers in the right direction on doing this with Matlab and using no loops.

Any help will be gr8.

thanks

shwaip
26 Apr 2007, 6:13pm
well, matlab treats all variables as matricies.

so if:
a=[1 2];
Then:
2*a = [2 4]
and (more usefully)
a.*a = [1 4]
or
a.^2 = [1 4]
Note that you have to use the .* operator, rather than the * operator to specify that you want matlab to multiply each element, rather than trying to do a matrix multiply. + and - have this behavior by default and ./ has behavior similar to .*

Hope that helps.

skillz
26 Apr 2007, 6:16pm
so if i treat my variables as matricies and manipulate them using element wise multiplication and devision, i would be heading in the right direction?

skillz
26 Apr 2007, 7:22pm
i think i may have a break through - would i need to use linspace at all to generate N points between xs and xe?

Does repmat come into play at any point also?

shwaip
26 Apr 2007, 11:03pm
you could use linspace or the colon ( : ) operator. The former is probably better.

You shouldn't need repmat, though.

skillz
26 Apr 2007, 11:51pm
you could use linspace or the colon ( : ) operator. The former is probably better.

You shouldn't need repmat, though.


i have managed to right this so far:

function [x, y, err] = eseries(xs, xe, ns, nt)

linspace = (xs, xe, ns)


But this only gives out a result in vector form - do i need to change this into matrix and manipulate it that way, i have been trying to do that but with no luck.
to manipulate linspace, i thought i would need to use repmat to replicate it and then use .* for some reason.

Am i going wrong? what could i think about doing next?

shwaip
27 Apr 2007, 7:54am
so linspace is a function (type help linspace).

x = linspace(0,1,10);

creates a vector called x which has 10 linearly spaced points between 0,1.

once you have that x vector, you could do whatever operations you want...

x.^2 would square every element in that matrix.


Also, remember that a vector is just a matrix with one dimension that is of size 1, so you don't need to "convert" a vector to a matrix.

I'm trying to be intentionally vague, because if I'm any more specific, I'll have done the problem for you...

skillz
27 Apr 2007, 9:54am
now that i have found out the values of x, i need to find out the values of the series.

this is where the x.^2 comes into play - i think, would i need to write out all the values i.e:

x.^2 + x.^3 + x.^4 + x.^5 ... x.^n

in a way that

x(1) =
x(2) =
x(3) =

but this seems very long winded, what if i had x(100) it would take ages!?

shwaip
27 Apr 2007, 4:24pm
lets say that you do this...

x=linspace(0,1,10); %x is a linearly spaced vector between 0,1

y = x.^2 + x.^3 + x.^4;

what does this give you if you run that code?

skillz
27 Apr 2007, 6:01pm
i get:

>> x = linspace(0,1,10)

x =

0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000

>> y = x.^2 + x.^3 + x.^4

y =

0 0.0139 0.0628 0.1605 0.3243 0.5754 0.9383 1.4414 2.1168 3.0000

skillz
27 Apr 2007, 6:04pm
although i have now tried to do something like this:

y = (x.^0/factorial(0)) + (x.^1/factorial(1)) + (x.^2/factorial(2))+ (x.^3/factorial(3)) + (x.^4/factorial(4))

shwaip
27 Apr 2007, 6:38pm
did you get what you expected from trying that?

skillz
27 Apr 2007, 10:49pm
did you get what you expected from trying that?

no, there is something not quite right, i just cant put my finger on it.