フィルターのクリア

%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

6 ビュー (過去 30 日間)
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;

回答 (1 件)

Raunak Gupta
Raunak Gupta 2020 年 3 月 17 日
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.
You can also use symbolic toolbox function expand for nicely displaying the expansion of (x+y)^n.
syms x y
n = 5;
p = (x + y)^n;
expand(p)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by