Multivariable Function from User Input; Pass Function to be Calculated
9 views (last 30 days)
Show older comments
Edited: Kingkeo Simlamany on 28 Jan 2022
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...]: ');
% Example value: 12
% Number of Current Sources
isPrompt= ('Enter all Current Sources values in array format[Is1 Is2 Is3...]: ');
% Example value: 3
% Number of Resistors
rPrompt= ('Enter all Resistor values in array format[R1 R2 R3...]: ');
% 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
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.
Max Heimann on 28 Jan 2022
Edited: Max Heimann on 28 Jan 2022
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)
This seems to work, even if input a function that only uses one or two of x,y,z.
More Answers (0)
Find more on Equation Solving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!