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.

回答(1 个)

Abraham Boayue
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 个评论
Abraham Boayue
Abraham Boayue 2018-3-11
Hey Houssem, Try the above and tell me what you think.
Abou Abd Almohsin Houssem
编辑:Abou Abd Almohsin Houssem 2018-3-11
I am very thankful that you are considering my problem. I do appreciate it. I will try the code that you provided.
I will appreciate also if you will try it on your own because I have some problems with my Matlab software on my PC, I do have just sort of verification tool in MS Excel ( a, b, c and d alongside the Ki s) just to see how the values fit the above conditions. That will be great.
Also, I have a little idea that may inspire you using another method that seems a little bit easier:
Instead of calculating the Ki s separately and find (a, b, c and d) that fit the conditions, the other way is to try to minimize the function "F" that equals to the sum of all the Ki s, with changing maximization of K1 to minimization by multiplying it by -1.
What do you think about it?
Thank you.
Regards.
Houssem.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by