Multivariable Function from User Input; Pass Function to be Calculated

6 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Max Heimann
Max Heimann le 28 Jan 2022
Modifié(e) : Max Heimann le 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)
result =
6
This seems to work, even if input a function that only uses one or two of x,y,z.
  1 commentaire
Kingkeo Simlamany
Kingkeo Simlamany le 28 Jan 2022
Modifié(e) : Kingkeo Simlamany le 28 Jan 2022
I had a guess that it would have been using such a format, but I wasn't entirely sure on the acceptable syntax used in str2func. This worked precisely how I needed it to, and allowed the program to work correctly and flawlessly. All that is left is for fine polishing to expand the utility, but overall functionality is complete. It can freely accept user input for all the Voltage Sources, Current Sources, and Resistors, then receive the Equations via user input as well, before finalizing calculation the node.
Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by