Is it possible to lock or fix an input value given by the user, inside a for loop?

dt=input('time differential = ')
segeval=input('seconds to evaluate = ')
nn=ceil(segeval/dt)
func(nn,1)=zeros
t(nn,1)=zeros
cont=0
for i=1:nn
t=cont
funcion=input('What function with respect to "t" do you wish to evaluate ')
func(i,1)=funcion
cont=cont+dt
end
% this is the function in this case 10*sin((pi*t)/0.6)
This code works in evaluating the function at each different value of "t" but the problem is that, since it is in the for loop it asks the user every cycle for the function again. Any idea on how to lock it in??

 Risposta accettata

Ask for the function first, before you enter the loop.

5 Commenti

I tried that as well but I get the following error:
Subscripted assignment dimension mismatch.
Error in Untitled2 (line 16) func(i,1)=funcion
What do you want your code to do? It is not obvious to me.
When you ask the user to give the function with respect to "T" (in this example 10*sin((pi*t)/0.6) ) it has to evaluate it for every "T" there is. So if the time differential 0.1 and the seconds to evaluate is 1, the function must iterate every "T" which in this case would be 0 0.1 0.2 0.3...1.0. The problem of putting the function outside the for is that it automatically wants to solve it for a value T wich does not exist yet because they are created inside the for loop.
I don't know if iam being clear, I appreciate the help
dt=input('diferencial de tiempo = ')
segeval=input('segundos a evaluar = ')
nn=ceil(segeval/dt)
func(nn,1)=zeros
t(nn,1)=zeros
cont=0
for i=1:nn+1
t(i,1)=cont
cont=cont+dt
end
funcion=input('introducir funcion de carga con respecto a "t" ')
% 10*sin((pi*t)/0.6)
That seemed to work, thank you anyway!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by