Printing pascals triangle binomial in loop

n = input('n:');
%Create first two rows that are always constant
pt(1, 1) = 1;
pt(2, 1 : 2) = [1 1];
for row = 3 : n
% First element of every row
pt(row, 1) = 1;
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for column = 2 : row-1
pt(row, column) = pt(row-1, column-1) + pt(row-1, column);
end
% Last element of every row
pt(row, row) = 1;
end
I have created the pascals triangle but I am trying to print the polynomial expansion of the value n. ie fprintf('(x+y)^3= x^3+3x^2y+x3xy^2+y^3');
the isuue is the value of n keeps on changing based on user input. Is there a way to create loop for such printing.

 Accepted Answer

Well, you have made a reasonable start, and all you really need is to find a way to output what you have done.
The conv trick here is a nice way to encapsulate the multiplies. It is a nice trick to learn for the future.
% preallocate the array of binomial polynomial coefficients
Pcoef = cell(n+1,1);
% the zero'th order polynomial is just 1 of course
Pcoef{1} = 1;
for m = 1:n
% see that conv does what is effectively the multiplication you need.
Pcoef{m+1} = conv(Pcoef{m},[1 1]);
end
% now build the desired polynomial as output
% we always know the first and last terms, and
% the second and penultimate terms are subtly different.
Pfinal = ['x^',int2str(n)'];
for m = 2:n
if m == 2
Pfinal = [Pfinal,' + ',int2str(Pcoef{end}(m)),'*x^',int2str(n-m+1),'*y'];
elseif m == n
Pfinal = [Pfinal,' + ',int2str(Pcoef{end}(m)),'*x*y^',int2str(m-1)];
else
Pfinal = [Pfinal,' + ',int2str(Pcoef{end}(m)),'*x^',int2str(n-m+1),'*y^',int2str(m-1)];
end
end
Pfinal = [Pfinal,' + 1'];
disp(Pfinal)
For example, with n = 5, we see
x^5 + 5*x^4*y + 10*x^3*y^2 + 10*x^2*y^3 + 5*x*y^4 + 1
The Pascal's triangle produced was:
Pcoef{:}
ans =
1
ans =
1 1
ans =
1 2 1
ans =
1 3 3 1
ans =
1 4 6 4 1
ans =
1 5 10 10 5 1

4 Comments

Thank you John. I am using an old textbook to learn and as it turns out I have not yet reached cells and coeffs function. Despite of that your code helped me to figure out and solve the issue using my current knowledge. Is there any way I can remove the extra zeros in my pascals triangle.
Pascal_t=zeros();
n = input('n:');
if n==1
Pascal_t(1,1) = 1;
fprintf('Pascals triangle: \n');
fprintf('%d \n',Pascal_t);
elseif n==2
Pascal_t(1,1) = 1;
Pascal_t(2,:) = 1;
fprintf('Pascals triangle: \n');
fprintf('%d ',Pascal_t);
else
Pascal_t(1,1) = 1;
Pascal_t(2,[1,2]) = 1;
for row = 3 : n+1
% First element of every row
Pascal_t(row, 1) = 1;
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for column = 2 : row-1
Pascal_t(row, column) = Pascal_t(row-1, column-1) + Pascal_t(row-1, column);
end
% Last element of every row
Pascal_t(row, row) = 1;
end
fprintf('Pascals triangle:\n');
Pascal_t(row, column) = Pascal_t(row-1, column-1) + Pascal_t(row-1, column)
end
fprintf('Expansion result:\n')
fprintf('(x+y)^%d = x^%d +',n,n);
a=1;
b=0;
k=n;
for index = 1:n-1
a = a+1;
k=n-1;
b=b+1;
fprintf('%dx^%d',Pascal_t(end,a),k);
fprintf('*y^%d+',b);
end
fprintf('y^%d',n);
Lol. That is why I used a cell array. It allows you to save things as a non-square array. If you are going to use regular (boring, dull) rectangular arrays, then you are stuck with what they bring to the problem.
I'm glad I was able to help you a bit though. Please do remember the conv trick I used, as it can be HUGELY valuable in the future. Conv is an amaingly useful tool.
Anyway, lets take a look at what you have. For n==5, I see:
Pascal_t =
1 0 0 0 0 0
1 1 0 0 0 0
1 2 1 0 0 0
1 3 3 1 0 0
1 4 6 4 1 0
1 5 10 10 5 1
Ok, so you were successful in creating the necessary array of coefficients, depite the zero padding. I would suggest that you just need to live with zero padding, at least until your coding knowledge expands to include tools like cell arrays. You could have also used struct arrays to solve the problem in a similar way. But if you don't know about cells, then structs are still in the future too. DRAT.
Next, looking at the polynomial you created, I see this:
(x+y)^5 = x^5 +5x^4*y^1+10x^4*y^2+10x^4*y^3+5x^4*y^4+y^5
Which looks decent to me, but not correct. You got the exponents wrong. In fact, it does point out that I made a mistake in the very last term myself. Silly of me. How would I fix the code you wrote though?
fprintf('Expansion result:\n')
fprintf('(x+y)^%d = x^%d + ',n,n);
for index = 1:n-1
a = n - index;
b = index;
fprintf('%dx^%d',Pascal_t(end,index+1),a);
fprintf('*y^%d + ',b);
end
fprintf('y^%d',n);
This produces:
Expansion result:
(x+y)^5 = x^5 + 5x^4*y^1 + 10x^3*y^2 + 10x^2*y^3 + 5x^1*y^4 + y^5
Which has the correct coefficients and exponents. Note that I added some extra white space, to make the distinct terms a bit more readable. White space is free, after all.
Finally, I want to still change it a bit more, since I don't like the x^1 and y^1 terms. So an if statement in there can fix that part. As well, I like to see a * in there to signify a multiply. It reminds you that this is MATLAB, which uses * to multiply. After all, you used * between the x and y sub-terms in what you wrote. But that is just my personal taste.
fprintf('Expansion result:\n')
fprintf('(x+y)^%d = x^%d + ',n,n);
for index = 1:n-1
a = n - index;
b = index;
if index == 1
% second term
fprintf('%d*x^%d',Pascal_t(end,index+1),a);
fprintf('*y + ');
elseif index == n-1
% penultimate term
fprintf('%d*x',Pascal_t(end,index+1));
fprintf('*y^%d + ',b);
else
% the general term
fprintf('%d*x^%d',Pascal_t(end,index+1),a);
fprintf('*y^%d + ',b);
end
end
fprintf('y^%d',n);
This did work to my infinitely picky satisfaction.
(x+y)^5 = x^5 + 5*x^4*y + 10*x^3*y^2 + 10*x^2*y^3 + 5*x*y^4 + y^5
But if you prefer the form you used, hey, whatever floats your boat is fine with me.
Thank you. This is way much better. It is because I still don't understand cells as it is two subtopics away and that is why the first one didn't work for me.
DUDE! YOUR SCRIPT FAILS MISERABLY AS SOON AS THE POWER INPUT IS A 2-DIGIT NUMBER! YOUR SCRIPT CAN ONLY DO POWERS UP TO 9. HOW DO YOU FIX THAT?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!