How to add more variables in script function file....
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I want to create a function handle in which 1200 variables are to be added....
function [f] = temporary(X)
f(1) = 15 * 0 * X(1) + 15 * 0 * X(2) + 15 * 0 * X(3) ....... + 20 * 4 * X(1198) + 20 * 5 * X(1199) + 20 * 3 * X(1200);
f(2) = 0 * X(1) + 0 * X(2) + 0 * X(3) + 0 * X(4) + 0 * X(5) + 0 * X(6) + 0 * X(7) + 0 * X(8) + 350 * X(9) + ...... + 1000 * X(1197) + 0 * X(1198) + 200 * X(1199) + 300 * X(1200);
end
But, when I copy paste the entire function line the Matlab shows and error and stops working... Then I have to restart.... Please tell me some another method to cope with this problem....
2 comentarios
Star Strider
el 24 de Jun. de 2017
‘...the Matlab shows and error and stops working...’
And the error is ... ?
Copy ALL the red text from your Command Window and paste it to a Comment here.
Respuestas (3)
dpb
el 24 de Jun. de 2017
Editada: dpb
el 24 de Jun. de 2017
Why in the world would you even attempt to write every element out explicitly for 1200 terms???? Pass the coefficients as well...
f(a,b,x)=sum(a.*b.*x);
ADDENDUM
Expanding on comment...I took the first 16 terms of your pasted expression...
s='f(1) = 15 * 0 * X(1) + 15 * 0 * X(2) + 15 * 0 * X(3) + 15 * 0 * X(4) + 15 * 0 * X(5) + 15 * 0 * X(6) + 15 * 0 * X(7) + 15 * 0 * X(8) + 15 * 4 * X(9) + 15 * 3 * X(10) + 15 * 2 * X(11) + 15 * 2 * X(12) + 15 * 0 * X(13) + 15 * 6 * X(14) + 15 * 3 * X(15) + 15 * 3 * X(16)';
s=allwords(s(findstr(s,'=')+1:end)); % split the string after the = sign to tokens
coeff=reshape(str2num(char([s(1:6:end)' s(3:6:end)'])),[],2)
coeff =
15 0
15 0
15 0
15 0
15 0
15 0
15 0
15 0
15 4
15 3
15 2
15 2
15 0
15 6
15 3
15 3
>>
And, as Jan points out, unless there's some other need for both coefficients, to evaluate the expression you only need the product to pass to the function.
The above is specific to the single line and uses basic string processing; it wouldn't be difficult to generalize to an array and directly parse with regexp for more production-like implementation.
But still, if you could get the code generator to spew out the coefficients instead and even better as numeric instead of text that would make it much easier/simpler.
1 comentario
dpb
el 24 de Jun. de 2017
"coffiecients are not written,,, but generated by a code."
Well, yes, we presumed that but it's wrongheaded approach to try to use in that form directly.
Can you not get the code generator to return the coefficient arrays directly? If not, then parse the string to reduce it to the coefficients first.
I don't know the line lengths limits of the Matlab parser but surely this will be a very inefficient method even if you can somehow smoosh your way around that.
Jan
el 24 de Jun. de 2017
Editada: Jan
el 24 de Jun. de 2017
How did you create this huge and unclear code?
Do not create it explicitely, but perform the multiplication dynamically. Store the data vectors in a MAT file. Then:
function f = temporary(X)
persistent Param
if isempty(Param) % Load it once only to save time
Param = load('C:\temp\YourDataFile.mat');
end
f = [Param.V1 * X(:), Param.V2 * X(:)];
end
Here V1 is e.g. the vector
[15 * 0, 15 * 0, 15 * 0, ..., 20 * 4, 20 * 5, 20 * 3]
as numners, so it starts with 0, not with 0*15, of course.
This is faster, more flexible, ways easier to read and does not crash Matlab.
0 comentarios
dpb
el 25 de Jun. de 2017
Editada: dpb
el 25 de Jun. de 2017
Just as confirmation you really, really, really don't want to do this, I took the above first 16 terms portion of the expression and ran some timing tests...
>> for i=1:13
tic
eval([s ';']);
fprintf('%d %.2f\n',length(s),f(1))
s=[s '+' s(7:end)];
toc
end
267 174.36
Elapsed time is 0.004928 seconds.
529 348.72
Elapsed time is 0.004600 seconds.
1053 697.44
Elapsed time is 0.008264 seconds.
2101 1394.88
Elapsed time is 0.014103 seconds.
4197 2789.75
Elapsed time is 0.037963 seconds.
8389 5579.50
Elapsed time is 0.061344 seconds.
16773 11159.01
Elapsed time is 0.123421 seconds.
33541 22318.01
Elapsed time is 0.269622 seconds.
67077 44636.02
Elapsed time is 0.652595 seconds.
134149 89272.05
Elapsed time is 3.710906 seconds.
268293 178544.09
Elapsed time is 16.538878 seconds.
536581 357088.19
Elapsed time is 71.505552 seconds.
1073157 714176.37
Elapsed time is 274.385861 seconds.
>>
Initially I ran the loop longer and went away...it had, indeed, crashed Matlab and the session closed so I'm not positive exactly between which length finally did cause the crash but confirmed that there is a length limit that will do so. I believe it was for i=15 where the length would have been 429613 by the way I was concatenating the strings. Wherever that limit actually is (and it may well be dependent on what else is consumed at the time as far as other resources so may not be at all reproducible between systems), the timings illustrate that the solution is totally unworkable from a practical standpoint far before you reach the length limit from the time standpoint.
I thought perhaps converting to a functional might help, but it took even longer for str2func to process the strings than to directly eval them although for the ones I tried the numerical results were the same.
The net conclusion is still that the above direct, "brute force" solution is just not practical. In a compiled language such as Fortran if the compiler will accept enough continuation lines to be able to compile the code it likely could be made to work but is still an abomination for such long expressions.
Just for grins, I plotted the time vs length...
1 comentario
dpb
el 25 de Jun. de 2017
Actually, though, on reflection the line lengths that caused problems here are like 2X or so larger than what one would guess OP's length is. The term '+ 20 * 3 * X(1200)' is 18 characters; 1200*18 --> 21,600. A 33,541 length str was 0.27 sec, before the real penalty hit at roughly 2X that.
As noted, perhaps the limits are different on different machine loadings, versions, etc., but unless there's one of those effects happening doesn't seem as though the shown sample strings should have crashed Matlab entirely.
Still not what I'd recommend to do to implement the computation, but...
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!