To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
skillz
Icrontic Convert
skillz
10 Posts

Question Matlab problem - Power Series implementation

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.

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

i heard that this can be done possible in as little as 5 lines of code!
shwaip
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

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...
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
skillz
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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).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
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

you could use linspace or the colon ( : ) operator. The former is probably better.

You shouldn't need repmat, though.
skillz
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

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
elaborate bot
shwaip
5,729 Posts

Re: Matlab problem - Can Anyone help?

did you get what you expected from trying that?
skillz
Icrontic Convert
skillz
10 Posts

Re: Matlab problem - Can Anyone help?

did you get what you expected from trying that?
no, there is something not quite right, i just cant put my finger on it.
Similar Threads
Thread Thread Starter Forum Replies Last Post
No Power to MoBo, Replaced PS w/650W still NO POWER, help. hbi1000 Mods & Cooling 5 20 Jun 2006 11:18am
pc is super slow panget Resolved / Inactive 0 23 Apr 2006 11:02am
Please help! panget Resolved / Inactive 0 20 Apr 2006 12:39pm
Possible Power Supply Problem Yzradxs Mods & Cooling 4 1 Sep 2003 12:24am
Whats wrong - power supply? CyrixInstead Mods & Cooling 4 10 Jun 2003 4:50am

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 1:00am (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.