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!!!!
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!!!!
0
Comments
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
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.
Does repmat come into play at any point also?
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?
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...
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!?
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?
>> 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
y = (x.^0/factorial(0)) + (x.^1/factorial(1)) + (x.^2/factorial(2))+ (x.^3/factorial(3)) + (x.^4/factorial(4))
no, there is something not quite right, i just cant put my finger on it.