How do i substitute data from a list to a summation
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
Hello I want to substitute multiple data from an excel worksheet(Vi,Xi) to this equation:
E := sum((V[i]-xi[i]*((1+b)/(b*xi[i]^4+1))^((1+b)/(4*b)))^2, i = 1 .. 106);
Do i need to create a loop (i=i+1)or can be done by using a simple function?
Réponses (1)
Star Strider
le 15 Oct 2014
It can be done essentially as you wrote it, without a loop, using element-wise operations:
V = randi(20,10,1); % Create Data
xi = randi(20,10,1); % Create Data
b = 5; % Create Data
E = sum(V - xi.*((1+b)./(b.*xi.^4+1)).^((1+b)./(4*b))).^2;
7 commentaires
Georgios Panagiotou
le 15 Oct 2014
Star Strider
le 15 Oct 2014
I was creating data assuming you had already imported your data. You did not attach your Excel worksheet, so I cannot post the exact code I would use to read it and then process the data.
See the documentation for xlsread for details on how to read your .xls file into your MATLAB workspace.
I always use the [num,txt,raw] = xlsread(_) syntax but there are several options.
Georgios Panagiotou
le 15 Oct 2014
Modifié(e) : Star Strider
le 15 Oct 2014
Georgios Panagiotou
le 15 Oct 2014
Star Strider
le 15 Oct 2014
I don’t understand what you’re doing. How do you define the derivative?
‘E’ looks like an expression for a least-squares cost function. Is it?
Georgios Panagiotou
le 16 Oct 2014
Star Strider
le 16 Oct 2014
I thought so after thinking about it later, but I wanted to be certain.
The easiest way to estimate ‘b’ is to use the fminsearch function:
fidi = fopen('BLADE.txt');
D = textscan(fidi, '%f %f', 'HeaderLines',1);
xi = D{1};
V = D{2};
Vest = @(b,xi) xi.*((1+b)./(b.*xi.^4+1)).^((1+b)./(4*b)); % Model
E = @(b) sum(V - xi.*((1+b)./(b.*xi.^4+1)).^((1+b)./(4*b))).^2; % Cost Function
[b,Eval] = fminsearch(E, 1); % Estimate ‘b’
figure(1)
plot(xi, V, '+b')
grid
hold on
plot(xi, Vest(b,xi), '-r', 'LineWidth',1)
hold off
xlabel('Xi')
ylabel('V')
The code first imports your data, and assigns variables. I created the objective function ‘Vest’ from ‘E’, then created ‘E’ as an ‘anonymous function’ that I could use with fminsearch to estimate ‘b’ and also determine the value of ‘E’ at the minimum, since you want that. I then plotted ‘Vest’ with your data in the figure. It appears to be a good fit. The estimated value for ‘b’ here is 1.4772.
Does this do what you want?
Cette question est clôturée.
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!