Help in coding using Matlab
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am new in this awesome forum, and I am also a very freshman in Matlab, and I have a little problem to solve, I used MS Excel but it didn't really work in this case.
The problem is:


I'm hoping you can help me with that problem, I really need help.
Thank you. Regards.
Houssem.
0 个评论
回答(1 个)
Abraham Boayue
2018-3-11
clear variables
close all
% 1. Load the data into matlab and extract the Y, Z and C components
load data.txt % Edite the data file before loading it into matlab, I had some
Y = data(:,1);% issues with it that needed fixing; delete all the letters
% and save only the numerical values.
Z = data(:,2);
C = data(:,3);
% 2. Process the data
n = length(C);
X = zeros(n,1);
a1 = 10;
step = a1/(n-1);
a = 0:step:a1;
b = 0:step:a1;
c = 0:step:a1;
d = 0:step:a1;
for ii = 1:n
X(ii) = a(ii) + b(ii)*(Y(ii)^c(ii)/Z(ii)^d(ii));
end
x_hat = mean(X);
c_hat = mean(C);
%%You can calculate vector values of the Ks, but this is unnecessary since
%%their values don not change.
% A = zeros(n,1);
% B = A;
% D = B;
% E = D;
% F = E;
% G = F;
% for i = 1:n
% A = A + (C(i)-c_hat)*(X(i)-x_hat);
% B = B + ((C(i)-c_hat)^2);
% D = D + ((X(i)-x_hat)^2);
% E = E +((C(i)-X(i))^2);
% F = F + abs(C(i)-X(i));
% G = G + (X(i)/C(i)-x_hat/c_hat).^2;
%
% R = sqrt(B*D');
% T = A/R(:,1);
% end
% K1 = T(:,1).^2;
% K2 = E/n;
% K3 = sqrt(K2);
% K4 = F/n;
% K5 = 100*sqrt(G/(n-1));
% 3. Calculate the values of the Ks
F1 = sum((C-c_hat)'*(X-x_hat));
F2 = sqrt(sum((C-c_hat).^2))*sqrt(sum((X-x_hat).^2));
K1 = (F1/F2).^2;
K2 = sum(((C-X).^2))/n;
K3 = sqrt(K2);
K4 = sum(abs(C-X))/n ; % or mean(abs(c-X));
K5 = 1/(n-1)*sum(((X./C)-(x_hat/c_hat)).^2);
K5 = 100*sqrt(K5);
% 4 Make tables of your calculations
fprintf('------------Table1: Some values of a,b,c and d -------\n')
fprintf(' a b c d \n')
fprintf('======================================================\n')
fprintf('%12.5f %12.5f %12.5f %12.5f \n',[a(1:6);b(1:6);c(1:6);d(1:6)]);
fprintf('======================================================\n');
fprintf(' \n')
fprintf('----Table2: value(s) of K1,K2, K3, K4 and K5 ------\n')
fprintf(' K1 \t K2 \t K3 \t K4 \tK5 \n')
fprintf('====================================================================\n')
%fprintf('%12.5e %12.5e %12.5e %12.5e %12.5e \n',[K1(1:6)';K2(1:6)';K3(1:6)';K4(1:6)';K5(1:6)']);
fprintf('%12.5f %12.5f %12.5f %12.5f %12.5f \n',[K1;K2;K3;K4;K5]);
fprintf('====================================================================\n');
% 5 Create plots, may not be necessary
plot([0,n-1],[K1,K1]/1e5,'k--'); hold on
plot([0,n-1],[K2,K2]/1e5,'b--');
plot([0,n-1],[K3,K3]/1e5,'r--');
plot([0,n-1],[K4,K4]/1e5,'m--');
plot([0,n-1],[K5,K5]/1e5,'c--');
ylim([-3,10]*1e5); % Adjust the y limites to get a better view
grid
a = legend('K1','K2','K3','K4','K5');
set(a,'fontsize',14)
a =title('The Kis');
set(a,'fontsize',14);
a = xlabel('Length');
set(a,'fontsize',14);
a = ylabel('Values');
set(a,'fontsize',14);
2 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!