Passing an integral function into another function that I wrote

I want to write a function called 'HW1A' that will take in the variables tau, I_max, and V_in. V_in would be another mathematical function in itself.
Basically I want to solve V_out using the equation V_out = 1/tau * [the integral of V_in with respect to time from 0 to t_f]. V_in should be an input to the function.
function V_out = HW1A(tau,I_max, V_in);
t_f = 1.5;
fun = V_in;
V_out = 1/tau * integral(fun, 0, t_f) ;
R = V_in / I_max;
C = tau / R;
plot(t, V_out);
title(['$ V_{out} $ for RC circuit $ (R=' num2str(R) '\Omega, C=' num2str(C) 'F) $ '], 'interpreter', 'latex');
xlabel('Time (sec)', 'interpreter', 'latex');
ylabel('$ V_{out} $ (V)', 'interpreter', 'latex');
grid on
Can someone tell me what I am doing wrong?
Thanks in advance!

7 件のコメント

Matt J
Matt J 2018 年 2 月 26 日
編集済み: Matt J 2018 年 2 月 26 日
Can someone tell me what I am doing wrong?
How would we know? You haven't shown how you are invoking HW1A nor copy/pasted any error messages.
positron96
positron96 2018 年 2 月 26 日
It gave me this error
Walter Roberson
Walter Roberson 2018 年 2 月 26 日
Note that V_in needs to accept a vector of input values and to give results for each of the inputs.
positron96
positron96 2018 年 2 月 26 日
May I ask how one would go about doing this?
Walter Roberson
Walter Roberson 2018 年 2 月 26 日
You posted that, "V_in would be another mathematical function in itself."
but in that input position, you passed 10, which is not a mathematical function.
If for some odd reason you really did intend to pass in the function which gives the constant output 10 no matter what the input, then you would call
HW1A(0.3, 0.001, @(x) 10 * ones(size(x)) )
positron96
positron96 2018 年 2 月 26 日
Basically, after inputting f(t) = 10, I was gonna try passing a step function then a triangle wave. would I have to change the third argument each time?
Walter Roberson
Walter Roberson 2018 年 2 月 26 日
"would I have to change the third argument each time?"
Yes.
An example of a step function would be @(x) floor(x/10)
An example of a triangle function would be @(x) abs(mod(x,4)-2)-1

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

質問済み:

2018 年 2 月 25 日

コメント済み:

2018 年 2 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by