%% Block Parameters - Throttle

% Conversion function
fcn_name = @sdl_carem_parameter_to_coefficient_throttle;

%% Parameter values in Block Parameters window
% Make sure the Block Parameters window is selected before pushing Get button.

 
blk = gcb;
thr_rest_blk = eval(get_param(blk, 'thr_rest'))
D_plate_blk = eval(get_param(blk, 'D_plate'))
S_leak_blk = eval(get_param(blk, 'S_leak'))

%
%% New parameter values
% Rest angle

theta_rest_deg = 3;

% Throttle plate diameter

D_plate_cm = 12;

% Throttle leakage area

S_leak_cm2 = 1;

%

 
x_deg = linspace(0, 90, 100);
x = x_deg/180*pi;  % degrees to radians

% Get coefficient values from parameters

k_blk = fcn_name(S_leak_blk, D_plate_blk, thr_rest_blk/180*pi);
y_blk = k_blk(1) + k_blk(2)*x + k_blk(3)*x.^2 + k_blk(4)*x.^3;

k = fcn_name(S_leak_cm2, D_plate_cm, theta_rest_deg/180*pi);
y_poly = k(1) + k(2)*x + k(3)*x.^2 + k(4)*x.^3;

figure
hold on

p1 = plot(x_deg, y_poly, 'LineStyle','-', 'LineWidth',2, ...
    'Color',[0 0.447 0.741]);  % default dark blue
p_blk = plot(x_deg, y_blk, 'LineStyle','-', 'LineWidth',2, ...
    'Color',[1 0 0]);

legend({'New','Block Parameters'}, 'Location','northwest')

grid on
xlabel('Throttle angle (deg)')
ylabel('Throttle open area (cm^2)')

%
%% Set new values to the Block Parameters

 
set_param(blk,'thr_rest', num2str(theta_rest_deg));
set_param(blk,'D_plate',  num2str(D_plate_cm));
set_param(blk,'S_leak',   num2str(S_leak_cm2));
open_system(blk);  % force refresh block dialog