%i created a fibonacci sequence but i want an expanded output of the polynmial that give the fibonacii sequence, %ie (x+y)^3 = x^3 + 3x^2y + 3xy^2 + y^3
4 views (last 30 days)
Show older comments
clc;
clear;
close all;
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
fprintf('Pascals triangle:\n');
pt(row, column) = pt(row-1, column-1) + pt(row-1, column)
fprintf('Expansion result:\n')
fprintf('(x+y)^%d =',n);
x=n;
y=0;
Answers (1)
Raunak Gupta
on 17 Mar 2020
Hi,
As per my understanding above code will give a pascal triangle if you store the value in pt as mentioned in the question. If the size of pt is (n+1)x(n+1) the last row will give the coefficient of polynomial expansion of (x+y)^n . For formatting the result correctly you can define two vectors with x exponent and y exponent respectively as n:-1:0 and 0:1:n and can format using for loop and sprintf and strcat.
syms x y
n = 5;
p = (x + y)^n;
expand(p)
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!