Conversion to function_handle from double is not possible.
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
t=0:0.01:10;
tou=0:0.01:10;
p0=input('Enter p0 =');
td=input('Enter value of td =');
k=input('Enter value of k=');
w=input('Enter value of w=');
for m=1:length(t)
for n = 1 : length(tou)
if tou(n)< td;
p(n) =p0*(tou(n)/td);
break;
else
p(n)=p0;
end
P=@(tou)p(n)*sin(w*(t-tou));
end
x(t)=(k/w)*quadgk(P,0,t);
end
plot(t, x(t));
0 Commenti
Risposte (1)
Geoff Hayes
il 29 Ago 2014
Nabhdeep - a couple of things. Typically when an error message is generated, the line number corresponding to the code (and the code itself) is written to the console along with the error message. That is useful information to include in your post. Also, when including code that requests user inputs, please include those values that you typed in when you ran the script.
The function handle P is initialized at
P=@(tou)p(n)*sin(w*(t-tou));
and used only at
x(t)=(k/w)*quadgk(P,0,t);
Both lines seem valid so I'm not sure what line of code is throwing your error message. But note the if condition and body
if tou(n)< td;
p(n) =p0*(tou(n)/td);
break;
If your choice of td is greater than 0 then we this condition will always be true on the first iteration of n, and so we will exit the for loop without ever initializing P. This will throw the Undefined function or variable "P" error when you try to evaluate guadgk. I also don't see why you need p(n) as an array since your P function only ever uses that from the most recent n. I suspect that you could replace the above with
for m=1:length(t)
pn = p0;
for n = 1 : length(tou)
if tou(n)<td
pn = p0*(tou(n)/td);
break;
else
pn = p0;
end
end
P=@(tou)pn*sin(w*(t-tou));
x(t)=(k/w)*quadgk(P,0,t);
end
So I've just replaced p(n) with a scalar pn. You should revisit the if tou(n)<td because this will always evaluate to true if your input td is positive (greater than zero)s once tou(1)==0.
As an aside, I would replace the input parameter of your anonymous function, tou, with some other name so as not to conflict with the local variable vector tou.
The above will work up until the point that quadgk is executed. It will generate the following error message
Error using quadgk (line 99)
A and B must be scalar floats.
This is because the third input to your function (which corresponds to B, see quadgk) is a vector and not a scalar. Perhaps what you want is
x(t)=(k/w)*quadgk(P,0,t(m));
While this gets rid of the above error, it will generate a new one
Error using quadgk/finalInputChecks (line 451)
Output of the function must be the same size as the input.
According to the documentation, your function P should accept a vector argument x and return a vector result y, where y is the integrand evaluated at each element of x. The problem may be due to the fact that your function uses t, which is a vector and not necessarily the same size as the input vector x. Should your function be
P=@(tou)pn*sin(w*(t(m)-tou));
instead?
Finally, since quadgk returns a scalar value, the assignment
x(t)=(k/w)*quadgk(P,0,t(m));
will fail with the
Subscript indices must either be real positive integers or logicals.
because of t having non-integer values.
You may want to use
x(m)=(k/w)*quadgk(P,0,t(m));
instead and then
plot(t, x);
Please review the above and implement changes as you see fit.
2 Commenti
Geoff Hayes
il 29 Ago 2014
Modificato: Geoff Hayes
il 29 Ago 2014
Running the above code, with your inputs, still fails with the error
Undefined function or variable 'P'.
because td is one, and so t1(1) which is zero is less than td. So we break out and the assignment for P does not occur. You should move this outside of the inner for loop. As well, I did not mean that you should change all instance of tou to something else, just as the input parameter to your anonymous function so that it does not confuse the reader when seeing that and the local variable of the same name.
There are still some discrepancies in the images you have attached and what you have written for code, so try to concentrate more on the integration part and how the p(t) function is used within it. I don't think that you need a loop to solve this problem, but you will have to define p(t) separately that takes as inputs p0 and td.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!