The Student Room Group

Matlab

Hey, this is probably a fairly basic Matlab question, but I just need some help doing this as I've been stuck for a while now.

I'm trying to write a function which will add polynomials of any degree. The input & the output polynomials should be in vector format.

I have done this, so far:

function f= polyadd2(a,b)

length_a=length(a);
length_b=length(b);
if length_a==length_b

f=a+b;

which is a function that adds polynomials which have the same degrees & lengths (2x-3 and 3x+4 for example).
My question is, how can I further alter my function in order to add polynomials which have the same degree but different lengths (2x-3 and 4x for example) and ones with different degrees and different lengths?
Any help will be greatly appreciated :biggrin:

edit: This is assuming that when you write the function 2x-3 you write it as [2,3] and when you write 4x you just write it as [4] instead of [4,0].
Reply 1
TI-84

edit: This is assuming that when you write the function 2x-3 you write it as [2,3] and when you write 4x you just write it as [4] instead of [4,0].

Do you have to use [4] to represent 4x?

Using your vector format, [4] could mean 4 or 4x or 4x^2... How will MATLAB know which one it is?
Reply 2
notnek
Do you have to use [4] to represent 4x?

Using your vector format, [4] could mean 4 or 4x or 4x^2... How will MATLAB know which one it is?


Well yes, in the example that I've given I have to represent [4] as [4x] the lecturer did something using the function "zeroes".
Reply 3
Do all universities use matlab? (sorry this isn't a very helpful comment XD)
Reply 4
TI-84
Well yes, in the example that I've given I have to represent [4] as [4x] the lecturer did something using the function "zeroes".

So what would 4x^2 be represented as?

Maybe I'm missing something here...

Latest