MATLAB Answers

How to make Subscript indices real positive integers or logicals?

1 view (last 30 days)
Nick
Nick on 9 May 2021
Edited: Nick on 14 May 2021
I wrote this code in order to create a neural network to approach the function f(x,y,z)=z^2 + x^3 -2xcos(yz+4) within [-1,1].
clc; clear;
x = -1:.01:1;
y = -1:.01:1;
z = -1:.01:1;
f=z.*z + x.*x.*x -2*x.*cos(y.*z +4);
Px=x; Py=y; Pz=z; P={Px;Py;Pz}; T=f;
net=newff([-1 1], [30,30,1], {'tansig','tansig','purelin'},'traingd');
net.trainParam.show = 200;
net.trainParam.lr = 0.01;
net.trainParam.epochs =5000;
net.trainParam.goal = 1e-5;
net.numinputs = 3;
trained = train(net,P, T);
N = 100;
Pxtest = 4*rand(1,N);
Pytest = 4*rand(1,N);
Pztest = 4*rand(1,N);
Ptest=[Pxtest;Pytest;Pztest];
fTest = f(Ptest);
testing= sim(trained,Ptest);
plot(Ptest,atest,'ro', Ptest,fTest,'b*'); grid;
legend('predicted value','actual value')
When I run it, I get the Error :
Subscript indices must either be real positive integers or logicals.
Error in approach_function(line 26)
fTest = f(Ptest);
How can I make that right?
  5 Comments

Sign in to comment.

Answers (2)

VBBV
VBBV on 9 May 2021
Edited: VBBV on 9 May 2021
clc; clear;
x = linspace(-1,1,100); %-1:.01:1;
y = linspace(-1,1,100); %-1:.01:1;
z = linspace(-1,1,100); %-1:.01:1;
f=z.*z + x.*x.*x -2*x.*cos(y.*z +4);
Px=x; Py=y; Pz=z; P={Px;Py;Pz}; T=f;
net=newff([-1 1], [30,30,1], {'tansig','tansig','purelin'},'traingd');
Warning: NEWFF used in an obsolete way.
See help for NEWFF to update calls to the new argument list.
net.trainParam.show = 50;
net.trainParam.lr = 0.01;
net.trainParam.epochs =1000;
net.trainParam.goal = 1e-5;
net.numinputs = 3;
%Train
net1 = train(net,P, T);
N = 100;
Pxtest = 4*rand(1,N);
Pytest = 4*rand(1,N);
Pztest = 4*rand(1,N);
Ptest=[Pxtest;Pytest;Pztest]
Ptest = 3×100
1.9924 3.0915 1.5216 2.7680 0.8887 0.2305 0.7168 2.8741 1.9135 1.4064 0.8084 2.9060 2.2433 1.6321 2.0365 2.9212 1.1595 2.4535 0.1054 0.8466 0.1808 3.8085 0.9260 1.3035 0.7274 2.0289 1.6223 1.3028 3.1772 0.1727 2.2631 0.3463 3.6635 1.5777 1.4688 3.6975 0.2912 1.2345 3.3546 2.8873 0.8192 0.5436 0.6928 2.0295 0.5443 3.7477 0.2396 3.3004 3.0682 1.4938 1.5573 2.3957 0.7689 1.8661 1.4758 1.9688 1.6184 0.9212 3.3036 3.1874 3.9679 3.8927 0.9101 3.1839 3.4496 3.4551 3.9787 2.9181 0.9469 3.8606 1.1151 0.9540 3.0542 2.3275 0.3336 2.6578 1.7286 1.0945 0.5705 0.5942 0.7805 3.7592 3.8752 1.7059 0.9889 3.1867 1.9154 3.6443 2.2380 0.4903
fTest = f.*(Ptest)
fTest = 3×100
1.1303 1.5447 0.6609 1.0282 0.2766 0.0585 0.1425 0.4219 0.1869 0.0723 0.0065 -0.0941 -0.1570 -0.1710 -0.2785 -0.4849 -0.2233 -0.5312 -0.0251 -0.2174 -0.0494 -1.0947 -0.2772 -0.4029 -0.2304 -0.6539 -0.5288 -0.4270 -1.0413 -0.0563 1.2839 0.1731 1.5912 0.5861 0.4571 0.9377 0.0579 0.1812 0.3276 0.1485 0.0066 -0.0176 -0.0485 -0.2126 -0.0744 -0.6221 -0.0461 -0.7145 -0.7299 -0.3836 -0.4256 -0.6886 -0.2302 -0.5768 -0.4674 -0.6345 -0.5275 -0.3019 -1.0827 -1.0391 2.2511 1.9451 0.3953 1.1827 1.0735 0.8762 0.7909 0.4284 0.0925 0.1985 0.0090 -0.0309 -0.2138 -0.2438 -0.0456 -0.4412 -0.3329 -0.2370 -0.1357 -0.1526 -0.2133 -1.0805 -1.1600 -0.5273 -0.3132 -1.0271 -0.6243 -1.1944 -0.7335 -0.1598
atest= sim(net1,Ptest)
atest = 1×100
1.5073 1.5073 1.5073 1.5073 1.2894 0.3378 1.1212 1.5073 1.5073 1.5073 1.3117 1.5073 1.5073 1.5073 1.5073 1.5073 1.5073 1.5073 0.2113 1.2805 0.2478 1.5073 1.3833 1.5073 1.1879 1.5073 1.5073 1.5073 1.5073 0.2378
mse = sum((fTest-atest).^2)/N;
fprintf('Mean square Error = %.4f\n', mse)
Mean square Error = 0.0075 Mean square Error = 0.0197 Mean square Error = 0.0196 Mean square Error = 0.0118 Mean square Error = 0.0177 Mean square Error = 0.0073 Mean square Error = 0.0220 Mean square Error = 0.0410 Mean square Error = 0.0514 Mean square Error = 0.0562 Mean square Error = 0.0510 Mean square Error = 0.0726 Mean square Error = 0.0815 Mean square Error = 0.0884 Mean square Error = 0.0810 Mean square Error = 0.1230 Mean square Error = 0.0879 Mean square Error = 0.1213 Mean square Error = 0.0106 Mean square Error = 0.0707 Mean square Error = 0.0075 Mean square Error = 0.1829 Mean square Error = 0.1183 Mean square Error = 0.1213 Mean square Error = 0.0701 Mean square Error = 0.1568 Mean square Error = 0.1283 Mean square Error = 0.1431 Mean square Error = 0.1822 Mean square Error = 0.0188 Mean square Error = 0.1544 Mean square Error = 0.0167 Mean square Error = 0.1414 Mean square Error = 0.1889 Mean square Error = 0.0312 Mean square Error = 0.1342 Mean square Error = 0.1023 Mean square Error = 0.1323 Mean square Error = 0.0186 Mean square Error = 0.1358 Mean square Error = 0.0975 Mean square Error = 0.0905 Mean square Error = 0.1099 Mean square Error = 0.0083 Mean square Error = 0.1090 Mean square Error = 0.0440 Mean square Error = 0.0107 Mean square Error = 0.0841 Mean square Error = 0.0758 Mean square Error = 0.0549 Mean square Error = 0.0659 Mean square Error = 0.0581 Mean square Error = 0.0138 Mean square Error = 0.0501 Mean square Error = 0.0340 Mean square Error = 0.0430 Mean square Error = 0.0393 Mean square Error = 0.0440 Mean square Error = 0.0349 Mean square Error = 0.0182 Mean square Error = 0.0280 Mean square Error = 0.0340 Mean square Error = 0.0062 Mean square Error = 0.0064 Mean square Error = 0.0072 Mean square Error = 0.0028 Mean square Error = 0.0197 Mean square Error = 0.0071 Mean square Error = 0.0261 Mean square Error = 0.0227 Mean square Error = 0.0260 Mean square Error = 0.0049 Mean square Error = 0.0044 Mean square Error = 0.0285 Mean square Error = 0.0426 Mean square Error = 0.0654 Mean square Error = 0.0802 Mean square Error = 0.0514 Mean square Error = 0.0299 Mean square Error = 0.0108 Mean square Error = 0.0113 Mean square Error = 0.0309 Mean square Error = 0.0705 Mean square Error = 0.0082 Mean square Error = 0.1339 Mean square Error = 0.1110 Mean square Error = 0.0776 Mean square Error = 0.0860 Mean square Error = 0.2498 Mean square Error = 0.2828 Mean square Error = 0.1514 Mean square Error = 0.0738 Mean square Error = 0.2114 Mean square Error = 0.0046 Mean square Error = 0.0802 Mean square Error = 0.0416 Mean square Error = 0.1018 Mean square Error = 0.2230 Mean square Error = 0.0679 Mean square Error = 0.4169
% Plot
plot(Ptest,atest,'ro', Ptest,fTest,'b*'); grid;
legend('predicted value','actual value')
  3 Comments
Nick
Nick on 9 May 2021
Thank you, I did, but I also want to plot the function with the predicted and actual value.
Why does it show me error when i run this?
plot(P,T,'r--',Ptest,atest,'ro', Ptest,fTest,'b*'); grid;
legend('Function','predicted value','actual value')
Error using plot
Invalid first data argument
Error in approach_function (line 33)
plot(P,T,'r--',Ptest,atest,'ro', Ptest,fTest,'b*'); grid;

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!