How to code an input function in script?
Mostrar comentarios más antiguos
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
Más respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!