How to code an input function in script?

조회 수: 108(최근 30일)
Lisanne van Osch
Lisanne van Osch 2021년 1월 10일
댓글: Lisanne van Osch 2021년 1월 13일
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
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 ;
%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)

채택된 답변

Monisha Nalluru
Monisha Nalluru 2021년 1월 13일
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
%display final required result using disp function
Hope this helps!
  댓글 수: 4
Lisanne van Osch
Lisanne van Osch 2021년 1월 13일
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!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by