Can any body solve this error? Subscript indices must either be real positive integers or logicals. Error in mynumericlalinverselaplace (line 17) u(i)=2/t*real(k1*F1(y(i),a1/t))+2/t*real(k2*F1(y(i),a2/t))...
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Farooq Aamir
il 11 Giu 2020
Risposto: John D'Errico
il 11 Giu 2020
clear all; close all; clc;
syms q;
a1=12.83767675+1i*1.666063445; k1=-36902.08210+1i*196990.4257;
a2=12.22613209+1i*5.012718792; k2=61277.02524-1i*95408.62551;
a3=10.93430308+1i*8.409673116; k3=-28916.56288+1i*18169.18531;
a4=8.776434715+1i*11.92185389; k4=4655.361138-1i*1.901528642;
a5=5.225453361+1i*15.72952905; k5=-118.7414011-1i*141.3036911;
Pr=10; t=2;
a=0.3;
y=linspace(0,5);
for i=1:100
F1{i}=@(q) sqrt((1-a)*q+a)/sqrt(Pr*q)-(sqrt((1-a)*q+a))*1/q*exp(-y(i)*sqrt((Pr*q^a)/(1-a)*q+a));
u(i)=2/t*real(k1*F1(y(i),a1/t))+2/t*real(k2*F1(y(i),a2/t))...
+2/t*real(k3*F1(y(i),a3/t))+2/t*real(k4*F1(y(i),a4/t))+2/t*real(k5*F1(y(i),a5/t));
end
plot(y,u);
axis([0 5 -9 6]);
2 Commenti
madhan ravi
il 11 Giu 2020
Modificato: madhan ravi
il 11 Giu 2020
You define F1 with one input argument but invoke with two?
F1(y(i),a1/t)) % error is here
Risposta accettata
John D'Errico
il 11 Giu 2020
Simple enough, really.
You create a cell array of function handles in a loop. Thus we see F1.
for i=1:100
for i=1:100
F1{i}=@(q) sqrt((1-a)*q+a)/sqrt(Pr*q)-(sqrt((1-a)*q+a))*1/q*exp(-y(i)*sqrt((Pr*q^a)/(1-a)*q+a));
u(i)=2/t*real(k1*F1(y(i),a1/t))+2/t*real(k2*F1(y(i),a2/t))...
+2/t*real(k3*F1(y(i),a3/t))+2/t*real(k4*F1(y(i),a4/t))+2/t*real(k5*F1(y(i),a5/t));
end
Then, as @madhan ravi points out, you use the function handles improperly. Actuallly, you still do not create them properly in what you claim fixes it.
Finally, we see the problem you mention, that you do not use those function handles correctly at all. For example, we see this:
F1(y(i),a1/t)
But F1 is NOT a function handle. F1 is a cell array, that just happens to contain function handles. There is a huge difference.
Why do you need to create that array of function handles anyway? You use it once, then not again. Perhaps you will use it later.
So, first, create each function handle CORRECTLY, to take TWO arguments. As you did it, this has ONLY ONE argument: q.
@(q) sqrt((1-a)*q+a)/sqrt(Pr*q)-(sqrt((1-a)*q+a))*1/q*exp(-y(i)*sqrt((Pr*q^a)/(1-a)*q+a));
However, you are calling it by passing in two arguments. That must fail. And I have no idea what you really want to do.
Next is the problem of indexing the cell array of function handles. I'll give an example of how to do that properly.
F1 = {@(x) x, @(x) x+1, @(x) x+2}
F1 =
1×3 cell array
{@(x)x} {@(x)x+1} {@(x)x+2}
>> F1{1}(2)
ans =
2
>> F1{2}(2)
ans =
3
>> F1{3}(2)
ans =
4
As you can see, F1 is a cell array that contains 3 function handles. If I want to now evaluate a specific function handle, use an index. Thus F1{1} accesses the first function handle. THEN we can evaluate that function handle. So to evaluate the i'th function handle you must use it as F1{i}(stuff), where stuff is whatever you will now pass into the function handle.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!