Why is my cod not working?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I wrote an interpolation code to get parameter called "landa" by just having two input parameters called "Alpha" and "Mu" respectively. Plese find attached the graph for which I am doing such interpolation. First, I fitted 6 degree polynomial to each alpha curve(please note that the number shown on each curve is Alpha). The coefficients of all curves are saved in "Coefficient " matrix (this also has been attached here). I am wondering why the cod is not working for some cases(e.g. suppose that we have Alpha=2, and Mu=2. In this case the code does not enter to the defined if condition!). can anyone help me with this regard?
clear
close all
clc
load("Coefficient.mat")
Alpha=2;
Mu=2;
%%%%%% calculating landa based on the coefficient matrix
Alpha_vector=Coefficient(:,2)+Coefficient(:,3)*Mu+Coefficient(:,4)*(Mu^2)+Coefficient(:,5)*(Mu^3)+Coefficient(:,6)*(Mu^4)...
+Coefficient(:,7)*(Mu^5)+Coefficient(:,8)*(Mu^6);
%%%%%%% if Alpha is exactly equall to one of graph values.
if Alpha==Coefficient(1,1)
landa=Alpha_vector(1);
elseif Alpha==Coefficient(2,1)
landa=Alpha_vector(2);
elseif Alpha==Coefficient(3,1)
landa=Alpha_vector(3);
elseif Alpha==Coefficient(4,1)
landa=Alpha_vector(4);
elseif Alpha==Coefficient(5,1)
landa=Alpha_vector(5);
elseif Alpha==Coefficient(6,1)
landa=Alpha_vector(6);
elseif Alpha==Coefficient(7,1)
landa=Alpha_vector(7);
elseif Alpha==Coefficient(8,1)
landa=Alpha_vector(8);
elseif Alpha==Coefficient(9,1)
landa=Alpha_vector(9);
elseif Alpha==Coefficient(10,1)
landa=Alpha_vector(10);
elseif Alpha==Coefficient(11,1)
landa=Alpha_vector(11);
elseif Alpha==Coefficient(12,1)
landa=Alpha_vector(12);
elseif Alpha==Coefficient(13,1)
landa=Alpha_vector(13);
elseif Alpha==Coefficient(14,1)
landa=Alpha_vector(14);
elseif Alpha==Coefficient(15,1)
landa=Alpha_vector(15);
elseif Alpha==Coefficient(16,1)
landa=Alpha_vector(16);
elseif Alpha==Coefficient(17,1)
landa=Alpha_vector(17);
elseif Alpha==Coefficient(18,1)
landa=Alpha_vector(18);
elseif Alpha==Coefficient(19,1)
landa=Alpha_vector(19);
elseif Alpha==Coefficient(20,1)
landa=Alpha_vector(20);
elseif Alpha==Coefficient(21,1)
landa=Alpha_vector(21);
elseif Alpha==Coefficient(22,1)
landa=Alpha_vector(22);
elseif Alpha==Coefficient(23,1)
landa=Alpha_vector(23);
elseif Alpha==Coefficient(24,1)
landa=Alpha_vector(24);
elseif Alpha==Coefficient(25,1)
landa=Alpha_vector(25);
elseif Alpha==Coefficient(26,1)
landa=Alpha_vector(26);
%%%%%% if Alpha are not something we see on the graph
elseif Alpha>0 && Alpha<0.1
landa=Alpha_vector(1)-((Alpha-0)*(Alpha_vector(1)-Alpha_vector(2))/0.1);
elseif Alpha>0.1 && Alpha<0.2
landa=Alpha_vector(2)-((Alpha-0.1)*(Alpha_vector(2)-Alpha_vector(3))/0.1);
elseif Alpha>0.2 && Alpha<0.3
landa=Alpha_vector(3)-((Alpha-.2)*(Alpha_vector(3)-Alpha_vector(4))/0.1);
elseif Alpha>0.3 && Alpha<0.4
landa=Alpha_vector(4)-((Alpha-.3)*(Alpha_vector(4)-Alpha_vector(5))/0.1);
elseif Alpha>0.4 && Alpha<0.5
landa=Alpha_vector(5)-((Alpha-.4)*(Alpha_vector(5)-Alpha_vector(6))/0.1);
elseif Alpha>0.5 && Alpha<0.6
landa=Alpha_vector(6)-((Alpha-.5)*(Alpha_vector(6)-Alpha_vector(7))/0.1);
elseif Alpha>0.6 && Alpha<0.7
landa=Alpha_vector(7)-((Alpha-.6)*(Alpha_vector(7)-Alpha_vector(8))/0.1);
elseif Alpha>0.7 && Alpha<0.8
landa=Alpha_vector(8)-((Alpha-.7)*(Alpha_vector(8)-Alpha_vector(9))/0.1);
elseif Alpha>0.8 && Alpha<0.9
landa=Alpha_vector(9)-((Alpha-.8)*(Alpha_vector(9)-Alpha_vector(10))/0.1);
elseif Alpha>0.9 && Alpha<1
landa=Alpha_vector(10)-((Alpha-.9)*(Alpha_vector(10)-Alpha_vector(11))/0.1);
elseif Alpha>1 && Alpha<1.2
landa=Alpha_vector(11)-((Alpha-1)*(Alpha_vector(11)-Alpha_vector(12))/0.1);
elseif Alpha>1.2 && Alpha<1.4
landa=Alpha_vector(12)-((Alpha-1.2)*(Alpha_vector(12)-Alpha_vector(13))/0.1);
elseif Alpha>1.4 && Alpha<1.6
landa=Alpha_vector(13)-((Alpha-1.4)*(Alpha_vector(13)-Alpha_vector(14))/0.1);
elseif Alpha>1.6 && Alpha<1.8
landa=Alpha_vector(14)-((Alpha-1.6)*(Alpha_vector(14)-Alpha_vector(15))/0.1);
elseif Alpha>1.8 && Alpha<2
landa=Alpha_vector(15)-((Alpha-1.8)*(Alpha_vector(15)-Alpha_vector(16))/0.1);
elseif Alpha>2 && Alpha<2.5
landa=Alpha_vector(16)-((Alpha-2)*(Alpha_vector(16)-Alpha_vector(17))/0.1);
elseif Alpha>2.5 && Alpha<3
landa=Alpha_vector(17)-((Alpha-2.5)*(Alpha_vector(17)-Alpha_vector(18))/0.1);
elseif Alpha>3 && Alpha<3.5
landa=Alpha_vector(18)-((Alpha-3)*(Alpha_vector(18)-Alpha_vector(19))/0.1);
elseif Alpha>3.5 && Alpha<4
landa=Alpha_vector(19)-((Alpha-3.5)*(Alpha_vector(19)-Alpha_vector(20))/0.1);
elseif Alpha>4 && Alpha<4.5
landa=Alpha_vector(20)-((Alpha-4)*(Alpha_vector(20)-Alpha_vector(21))/0.1);
elseif Alpha>4.5 && Alpha<5
landa=Alpha_vector(21)-((Alpha-4.5)*(Alpha_vector(21)-Alpha_vector(22))/0.1);
elseif Alpha>5 && Alpha<5.5
landa=Alpha_vector(22)-((Alpha-5)*(Alpha_vector(22)-Alpha_vector(23))/0.1);
elseif Alpha>5.5 && Alpha<6
landa=Alpha_vector(23)-((Alpha-5.5)*(Alpha_vector(23)-Alpha_vector(24))/0.1);
elseif Alpha>6 && Alpha<7
landa=Alpha_vector(24)-((Alpha-6)*(Alpha_vector(24)-Alpha_vector(25))/0.1);
elseif Alpha>7 && Alpha<8
landa=Alpha_vector(25)-((Alpha-7)*(Alpha_vector(25)-Alpha_vector(26))/0.1);
end
2 Kommentare
Walter Roberson
am 18 Jan. 2023
I suggest you replace the whole
if Alpha==Coefficient(1,1)
chain with
[wasfound, idx] = ismember(Alpha, Coefficient(1:26,1));
if wasfound
landa = Alpha_vector(idx);
else
the other stuff
end
Walter Roberson
am 18 Jan. 2023
Your other code looks like it could be done with
interp1([0:0.1:1, 1.2:0.2:2, 2.5:0.5:6, 7, 8], Alpha_vector, Alpha)
or something similar.
Antworten (1)
Walter Roberson
am 18 Jan. 2023
elseif Alpha>1.4 && Alpha<1.6
landa=Alpha_vector(13)-((Alpha-1.4)*(Alpha_vector(13)-Alpha_vector(14))/0.1);
The division should not be by 0.1 each time; it should be by the difference between the endpoints of the interval.
I suspect you could do the whole thing with an interp1() call, all of the branches of both cases.
3 Kommentare
Walter Roberson
am 18 Jan. 2023
Observe:
format long g
load Coefficient
Coefficient(1:10,1)
Coefficient(1:10,1) - (0:.1:.9).'
(Coefficient(1:10,1) * 10 - (0:9).')/10
Coefficient(1:10,1) - (0:9).'/10
Different ways of calculating the 0, 0.1, 0.2, ... give different exact results -- and none of them match exactly what you have in your file.
num2hex(Coefficient([4,5,9,10],1))
num2hex([0.3;0.4;0.8;0.9])
lin = (0:9)/10;
num2hex(lin([4,5,9,10]))
incr = (0:.1:.9).';
num2hex(incr([4,5,9,10]))
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!