How to code an input function in script?

338 visualizaciones (últimos 30 días)
Lisanne van Osch
Lisanne van Osch el 10 de En. de 2021
Comentada: Lisanne van Osch el 13 de En. de 2021
Hello all,
For my course we're learning how to write programs for Matlab, and I wanted to see what I could come up with and what I could program.
I'm trying to write a program that asks the user to input a function (expressed in x) (the one I'm running tests with is 1.35*(sqrt(x)) ), and to define x and then the program runs the calculation. I've already read (and tried) a couple of things on the forum, but everytime I try something, I get new errors. So I just took everything out concerning the input function and I am asking you; How do I write (in the script) the input for the user's function. I want it to work afterwards.
I tried str2func (in several different ways) and I tried inline & feval (but I don't understand those, so it's impossible to know what I'm doing there).
I'm using MatLab R2020b (academic use), and I have installed all toolboxes & extras.
Here is my code, it's in Dutch but there's English explanation as well:
%User inputs starting point/variable x, a function and an end condition.
%The program puts x through the function, and checks if the result matches
%or surpasses the end condition. If yes, done. If no, it puts the result
%through the function until it does match or surpass the end condition.
%So I want: input x, input function, input end condition, and a working
%recursion
clear, clc
%uitleg / explanation of the program
disp('Dit programma behoeft één variabele, een functie en één eindwaarde.')
disp('Vervolgens voert het programma de beginwaarde in, in de functie,')
disp('en zal het herhaaldelijk het resultaat opnieuw door de functie voeren,')
disp('totdat de eindwaarde behaald is.')
%invoer functie en variabelen / Input function and variables
f = input('Voer hier de functie in welke gebruikt moet worden, definieer alle bekende variabelen behalve x: ','s') ;
%This is the function input
x = input('Voer hier de beginwaarde in: ') ;
%This is the x, starting point, input
eind = input('Voer hier de eindwaarde in: ') ;
%This is the end condition input
%de recursion teller / Recursion Counter
N = 0 ;
%de recursion / The Recursion
while (x < eind) %As long as x is smaller than the end condition, the recursion needs to happen
% y = func ; Here I need the function to work
x = y ; %The result (y) of the function y = f(x) needs to go through the recursion again
%My idea was to define the new x as y
N = N+1 ;
end
%de resultaten / The Results
disp('De uitkomst, gelijk aan of hoger dan de ingevoerde eindwaarde, is: ')
disp (y)
disp('Het aantal recursions dat heeft plaatsgevonden is: ')
disp (N)

Respuesta aceptada

Monisha Nalluru
Monisha Nalluru el 13 de En. de 2021
From my understanding, you wanted to take a function, inital condition and final condition from user and process the values accordingly.
Inorder to process the function with user given inital conditions, the input function need to convertd to function handle using str2func which converts string to function handle
Here is an example
f=input("Enter function");
x=input("Enter inital condition");
eind=input("Enter end condition");
f="@(x)"+f; % making the user function to turn into function handle format
f=str2func(f) % f is now function handle
while(x<eind)
y=f(x);
x=y;
end
%display final required result using disp function
Hope this helps!
  4 comentarios
Monisha Nalluru
Monisha Nalluru el 13 de En. de 2021
the function need to be taken in form of string so
f=input("Enter function",'s');
Inside the while loop I am not sure what operations you want to perform
The reason for infite loop with function @(x)2.7 is 2.7 is constant value for each value of x, y would be same value which doesnot meet the condition
If you know the end condition is never meet then x=x+1 would be better choice or any other operation as required
Lisanne van Osch
Lisanne van Osch el 13 de En. de 2021
It works!
So what it does now is it puts the initial x into the function and the result becomes the new x until the result is equal to or larger than the end condition the user gave. That is exactly what I wanted.
I know the program sounds a bit nonsensical, but I just wanted to see if it would work. And it does now, so thank you so much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by