matlab菜鸟级问题求解答
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/16 23:01:30
matlab菜鸟级问题求解答
Alice buys three apples,a dozen bananas,and one cantaloupe for $2.36.Bob buys a dozen apples and two cantaloupes for $5.26.Carol buys two bananas and three cantaloupes for $2.77.How much do single pieces of each fruit cost?
•Set this up as a system of 3 linear equations.Encode these 3 linear equations in one matrix M,and one column vector V.Each equation should take the form:
◦ax + by + cz = price,where a is the number of apples,b the number of banans,c the number of cantaloupe
•Solve for each of the 3 variables (x,y,z) using the matrix left-division operator in Matlab (Gaussian elimination) and store in the prices variable.
•Complete the function,fruit_price,that takes 3 amount values (apples,bananas,cantaloupe) and computes the price for this purchase using the values in your prices variable.Your function should pass the following test:
◦assertEqual(fruit_price(4,6,3),4.13)
•Add three more tests that verify that your function correctly computes the values in the initial word problem.
%M =
%V =
%prices =
function out = fruit_price(apples,bananas,cantaloupe)
out = 0; % YOU NEED TO COMPUTE CORRECT OUT VALUE
end
assertEqual(fruit_price(4,6,3),4.13)
% write 3 tests for the initial system
Alice buys three apples,a dozen bananas,and one cantaloupe for $2.36.Bob buys a dozen apples and two cantaloupes for $5.26.Carol buys two bananas and three cantaloupes for $2.77.How much do single pieces of each fruit cost?
•Set this up as a system of 3 linear equations.Encode these 3 linear equations in one matrix M,and one column vector V.Each equation should take the form:
◦ax + by + cz = price,where a is the number of apples,b the number of banans,c the number of cantaloupe
•Solve for each of the 3 variables (x,y,z) using the matrix left-division operator in Matlab (Gaussian elimination) and store in the prices variable.
•Complete the function,fruit_price,that takes 3 amount values (apples,bananas,cantaloupe) and computes the price for this purchase using the values in your prices variable.Your function should pass the following test:
◦assertEqual(fruit_price(4,6,3),4.13)
•Add three more tests that verify that your function correctly computes the values in the initial word problem.
%M =
%V =
%prices =
function out = fruit_price(apples,bananas,cantaloupe)
out = 0; % YOU NEED TO COMPUTE CORRECT OUT VALUE
end
assertEqual(fruit_price(4,6,3),4.13)
% write 3 tests for the initial system
It's a very easy problem if you have learned Matlab before .
The answer is as following.
M=[3 ,12,1;
12,0,2;
0,2,3];
V=[3.26;5.36;2.77];
prices=M\V;
assertEqual(fruit_price(4,6,3),4.13)
Save the following in fruit_price.m:
function out=fruit_price(apples,bananas,cantaloupe)
out = 3.26*apples+5.36*bananas+2.77*cantaloupe;
% YOU NEED TO COMPUTE CORRECT OUT VALUE
end
The answer is as following.
M=[3 ,12,1;
12,0,2;
0,2,3];
V=[3.26;5.36;2.77];
prices=M\V;
assertEqual(fruit_price(4,6,3),4.13)
Save the following in fruit_price.m:
function out=fruit_price(apples,bananas,cantaloupe)
out = 3.26*apples+5.36*bananas+2.77*cantaloupe;
% YOU NEED TO COMPUTE CORRECT OUT VALUE
end