2 values for inputs of a function

If i have a function
example:
function(T,L)=answ(b,c,d)
L= b + c + d
T= 2*L
end
and i have 2 of c which are 25 and 60 1 of b which is 5 and 0 of d
where b c and d have their own equation
How can i input 2 values for c?

回答 (1 件)

Stephan
Stephan 2020 年 11 月 17 日
編集済み: Stephan 2020 年 11 月 17 日

0 投票

In case of a vectorized function (yours is) you can use vector inputs:
b = 5;
c = [25; 60];
d = 0;
[T, L] = answ(b,c,d)
function [T, L] = answ(b,c,d)
L= b + c + d;
T= 2*L;
end
gives vector outputs:
T =
60
130
L =
30
65

6 件のコメント

Jane Smith
Jane Smith 2020 年 11 月 17 日
Maybe i didnt explain my self right. Since i have 2 values of c i need L to be example b + c1 + c2 + d but i can have any amount of all 3.
So i can have 3 b and 0 c and 2 d for example.
Example 3 books, 0 carts and 1 desk
function [T,L] = answ(k,am,D,C,B)
L = book(B) + cart(C) + desk(D);
T= 2*L + am*4 + k*3;
function[b]=book(B)
b=B*4.2;
end
function[c]=cart(C)
c=2*C^2;
end
function[d]=desk(D)
d=D*1.30;
end
end
This is what i tried
but when then i tryied to input >>answ(1,3,3,-,2)
for C since i have 2 i didnt know what to enter as value
Stephan
Stephan 2020 年 11 月 17 日
Maybe working with the sum:
B = 5;
C = [25; 60];
D = 0;
k = 1;
am =1;
[T,L] = answ(k,am,D,C,B)
function [T,L] = answ(k,am,D,C,B)
L = sum(book(B) + cart(C) + desk(D));
T= 2*L + am*4 + k*3;
end
function b=book(B)
b=sum(B*4.2);
end
function c=cart(C)
c=sum(2*C.^2);
end
function d=desk(D)
d=sum(D*1.30);
end
Jane Smith
Jane Smith 2020 年 11 月 17 日
Didnt work
Stephan
Stephan 2020 年 11 月 17 日
編集済み: Stephan 2020 年 11 月 17 日
For me it worked. Whats the issue?
Jane Smith
Jane Smith 2020 年 11 月 17 日
Error: File: answ.m Line: 8 Column: 27
Function 'answ' has already been declared within this scope.
Stephan
Stephan 2020 年 11 月 17 日
Comment out old code or use my in a new script. Copy the whole code and run it.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

質問済み:

2020 年 11 月 17 日

コメント済み:

2020 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by