Multivariable Function from User Input; Pass Function to be Calculated
3 ビュー (過去 30 日間)
古いコメントを表示
First, are segments of my code that are being used in the part that I am having trouble with:
% Number of Voltage Sources
vsPrompt= ('Enter all Voltage Source values in array format[Vs1 Vs2 Vs3...]: ');
vV= input(vsPrompt);
% Example value: 12
% Number of Current Sources
isPrompt= ('Enter all Current Sources values in array format[Is1 Is2 Is3...]: ');
iV= input(isPrompt);
% Example value: 3
% Number of Resistors
rPrompt= ('Enter all Resistor values in array format[R1 R2 R3...]: ');
rV= input(rPrompt);
% Example values: [4 3 8 5]
%%---- WHERE I NEED HELP ----%%
% Request Function for a Matrix Element
i11= input('Enter the function for I11: ', 's');
iM11= str2func(['@(vV)' i11]);
% Example Function: vV(1,1)/rV(1,2)
% Calculation Function and Assign to Matrix Element
iMtrx(1,1)= iM11(vV);
Now, for the actual inquiry:
I need help being able for the last two parts to be capable of acknowledging that 'vV', 'iV', and 'rV' are three different variables, and may be used in the final computation. I've already checked that using a single variable in the function in the format of "rV(1,2)" works, but I don't know what process I need for this last part of my code. I have been able to resolve most of the issues I've encountered from other community posts, but I haven't been able to find an answer to this particular matter.
What is the code for?
I'm an Electrical Engineering student, and my professors have provided example code for calculating our homework and other assigned problems, but I am trying to make a permanent code to solve my circuit problems, based on user input. I imagine this could either save time with my calculations, or take about the same amount of time, but save potential headaches from accidentally making coding mistakes.
0 件のコメント
採用された回答
Max Heimann
2022 年 1 月 28 日
編集済み: Max Heimann
2022 年 1 月 28 日
Its an interesting problem. Generally if you just want to solve some circuit problems with matlab,i would just use a script where i adjust the voltage/resistor vectors and type out the function as code. Especially if its related to homework, i would assume that your tasks wont be so repetetive, that you can use the same user interface every time. Some adjustements will be made for each new assignement.
Now for trying to still solve your problem:
x = [1 2 3];
y = [2 3 4];
z = [4 5 6];
% Request user input
funcString = input('Enter the function for I11: ', 's');
anonFunc= str2func(['@(x,y,z)' funcString]);
result = anonFunc(x,y,z);
>> Enter the function for I11: x(3) + y(2)
result =
6
This seems to work, even if input a function that only uses one or two of x,y,z.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!