??? Error using ==> mtimes Inner matrix dimensions must agree. Error in ==> Untitled2 at 16 l0=[(x-15)​*(x-25)*(x​-35)*(x-45​)]/240000;

x=(0:1:40);
l0=((x-15)*(x-25)*(x-35)*(x-45))/240000;
l1=-(((x-5)).*((x-25)).*((x-35))*((x-45)))/60000;
l2=((x-5)*(x-15)*(x-35)*(x-45))/40000;
l3=-((x-5)*(x-15)*(x-25)*(x-45))/60000;
l4=((x-5)*(x-15)*(x-25)*(x-35))/240000;
plot(x,l0,'b',x,l1,'r',x,l2,'y',x,l3,'g',x,l4,'m')

Answers (2)

Change the matrix operator * to element-wise operator .*
E.g.,
x=(0:1:40);
l0 = ((x-15).*(x-25).*(x-35).*(x-45))/240000;
l1 = -(((x-5)).*((x-25)).*((x-35)).*((x-45)))/60000;
l2 = ((x-5).*(x-15).*(x-35).*(x-45))/40000;
l3 = -((x-5).*(x-15).*(x-25).*(x-45))/60000;
l4 = ((x-5).*(x-15).*(x-25).*(x-35))/240000;
You need to vectorise all operations in your calculations.
For example:
l0=((x-15).*(x-25).*(x-35).*(x-45))/240000;
See Array vs. Matrix Operations for the details.
I would have preferred that you had named your variables starting with something other than a lower-case ‘l’, since it can be confused with 1.

Tags

Asked:

on 18 Nov 2015

Answered:

on 18 Nov 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!