写了个算分压电阻阻值的MATLAB小程序
最近常遇到板子上电源部分需要几个电阻分压器的情况,写了个小程序来实现在指定电阻阻值系列内(E6~E192),选择尽量少的电阻种类实现需要的分压比例。
测试于MATLAB 2012a/2014b和Octave。
程序第一部分定义了需要计算的分压比例、对应的允许误差、系统中已经存在的固定电阻、阻值系列、最大迭代次数。
之后生成电阻系列的所有阻值。E48~E192系列的阻值基本是按照对数排列的,用logspace生成即可;E6~E24系列是用E24的取值间隔选取得到的。
把阻值系列中所有可以得到的分压比例,按对目标分压比例的误差排列,选择出符合误差要求的部分。
最后对这些在误差范围内的选择做随机搜索,查找使用尽量少数量的电阻种类能够完成的一种组合。
%% Optimize Resistance Dividers
% K.O.Carnivist
% 2015.6.18%% Input: the only cell to defined by users
% Define devider ratio values to solve and corresponding error limits.
% For each row, [Devider ratio, Max error].
input_list = ...[5.0 / 0.8 - 1, 0.02; ...1.0 / 0.6 - 1, 0.02; ...1.8 / 0.83 - 1, 0.02; ...2.5 / 0.6 - 1, 0.02; ...3.3 / 0.6 - 1, 0.02];% Define fixed resistors included in the system.
fixed_list = ...[12];% Resistor series.
% Available values: 6, 12, 24, 48, 96, 192.
res_series_name = 48;% Maximum number of iterations of the optimization loop
max_iter = 100000;%% Calculate resistor series
% res_series stores the available values of resistors in a column.
if res_series_name >= 48res_series = floor(logspace(2, 3, res_series_name + 1) + 0.5)';res_series = res_series(1: end - 1);res_series(res_series == 919) = 920;
elseres_series = [100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910]';res_series = res_series(1:24/res_series_name:24);
endclear res_series_name;%% Calculate resistance divider pairs
results = []; % Each cell stores the available value pairs of a divider (two same values for fixed resistors).
errors = []; % Each cell stores the corresponding relative error (zeros for fixed resistors).% Find available value pairs of dividers.
for i = 1:size(input_list, 1)% These are temporary variables.divider_ratio = input_list(i, 1);max_error = input_list(i, 2);% R1 and R2 are vectors available values in the spedified series. Divider = R1 / R2.% Normalize R1 or R2 if the ratio is too small or too large.if (divider_ratio >= 1)R1 = kron(res_series, 10 .^ (floor(log10(divider_ratio)-1):floor(log10(divider_ratio)+1)));R1 = R1(:);R2 = res_series;elseR1 = res_series;R2 = kron(res_series, 10 .^ -(floor(log10(divider_ratio)-1):floor(log10(divider_ratio)+1)));R2 = R2(:);end% Build an array of all the available pairs in the resistor series.R1_temp = kron(R1, ones(size(R2)));R2 = kron(ones(size(R1)), R2);R1 = R1_temp;clear R1_temp;divider_array = R1 ./ R2;% Sort by error.[Y, I] = sort(abs(log(divider_array ./ divider_ratio)));% Add valid pairs and errors.disp_num = find(Y <= log(1 + max_error), 1, 'last');results{i} = uint32([R1(I(1:disp_num)), R2(I(1:disp_num))]);errors{i} = Y(1:disp_num);
end
clear res_series divider_ratio max_error R1 R2 divider_array Y I disp_num;% For fixed resistors.
for i = 1:length(fixed_list)results{size(input_list, 1) + i} = [fixed_list(i), fixed_list(i)];errors{ size(input_list, 1) + i} = 0;
end%% Find the optimized selection of resistors
% Initialize
num_of_pairs = uint32(size(results, 2));
num_of_input = uint32(zeros(1, num_of_pairs));
now_index = uint32(zeros(1, num_of_pairs));
R_output = uint32(zeros(num_of_pairs, 2));
error_output = zeros(num_of_pairs, 1);R_max = 0;
for i = 1:num_of_pairsR_max = max(R_max, max(max(results{i})));
end
for i = 1:num_of_pairsfor j = 1:size(results{i}, 1)results{i} = [results{i}; ...kron(double(results{i}(j, :)), ...10 .^ (1:floor(log10(double(R_max) / ...double(results{i}(j, 1)))))')];errors{i} = [errors{i}; ...kron(errors{i}(j), ...ones(floor(log10(double(R_max) / ...double(results{i}(j, 1)))), 1))];end
end
clear R_max;for i = 1:num_of_pairsnum_of_input(i) = size(results{i}, 1);now_index(i) = randi(double(num_of_input(i)));R_output(i, :) = results{i}(now_index(i), :);error_output(i) = errors{i}(now_index(i));
endnow_num_of_res = num_of_pairs * 2 + 1;
now_max_error = inf;% Loop
count = uint32(0);
count_to_disp = 1;
tic;
for count = 1:max_iter% Select one pair of resistors to changeindex_to_change = randi(double(num_of_pairs));% Randomnow_index(index_to_change) = randi(double(num_of_input(index_to_change)));R_output(index_to_change, :) = results{index_to_change}(now_index(index_to_change), :);error_output(index_to_change) = errors{index_to_change}(now_index(index_to_change));% Check if it is the optimized selectionif (length(unique(R_output)) < now_num_of_res) || ...((length(unique(R_output)) == now_num_of_res) && (max(error_output) < now_max_error))now_num_of_res = length(unique(R_output));now_max_error = max(error_output);fprintf('Result of %g resistors in %g attempts:\n', now_num_of_res, count);for i = 1:num_of_pairsfprintf('%8d\t%8d\t%8.2f%%\n', R_output(i, 1), R_output(i, 2), error_output(i) * 100);endendif (count >= count_to_disp)fprintf('loop %g in %g seconds.\n', count, toc);count_to_disp = count_to_disp * 10;end
endclear i j;
clear num_of_pairs num_of_input;
clear now_index now_num_of_res now_max_error;
clear R_output error_output;
clear count count_to_disp max_iter;
clear index_to_change;
clear R_output error_output
写了个算分压电阻阻值的MATLAB小程序相关推荐
- 怎样在matlab q-q图上读出斜率,Q分解法潮流计算matlab小程序
<Q分解法潮流计算matlab小程序>由会员分享,可在线阅读,更多相关<Q分解法潮流计算matlab小程序(18页珍藏版)>请在人人文库网上搜索. 1.Q分解法潮流计算matl ...
- 深夜脑洞,写了一个可以推算火车票身份证号码的小程序
深夜脑洞,写了一个可以推算火车票身份证号码的小程序 1. 火车票上*号打的是月,日,理论上的有最大366种组合: 2. 校验码是最后的一位,0-9及X,11个结果: 3. 那么,通过火车票上的身份证号 ...
- uniapp 手写canvas海报(兼容android/ios/h5/微信小程序)
先上成功图 1.在父组件里面定义弹出层,并且调用子组件制作海报的方法 2.点击显示二维码调用子组件海报方法 showPoster(customerPostId) { // co ...
- python大乐透号码生成器_国庆长假写了个体彩大乐透彩票号码生成器小程序
国庆假期,除了带娃,就是回复客人和工厂邮件,还有看书了. 带娃大家都懂得,惨绝人寰,(好吧,女儿还是挺可爱的,但是精力太充沛了,自愧不如啊)比起来工作和看书就是天堂了... 不过假期有点长,后面几天思 ...
- 5 拦截器拦截请求路由_手写简易版axios拦截器,实现微信小程序wx.request的封装与拦截...
前言: axios是一个功能强大的网络请求库,其中拦截器又是axios的精髓.在小程序的开发或者需要手动实现ajax的时候,没有实现对请求的拦截,开发的时候非常不方便,因此手写一个简易版的axios拦 ...
- zanli_android_1.1.0,【轻松集赞】写了个涉嫌混淆微信官方服务的小程序
发生背景: 随着现在国内的社交软件用户群体的不断扩大,商家打广告的方式(套路)也越来越多了,每次走在大街上都可以看到商家打出来广告牌,"朋友圈点赞超过30享受六折优惠".在上一次和 ...
- 如何写一个能被手机打开的C语言小程序,如何用C语言中一些简单的语句做一个小程序,能够输入一个字符就会弹出一句话...
满意答案 lyj1260 2015.03.28 采纳率:43% 等级:11 已帮助:6408人 这个不难,是最基本的C语言程序了,我写个示例给你 #include //包含头 int main( ...
- 写一款,汽车养护宝,通过微信小程序来管理汽修店的顾客,提醒他们来店保养,array_change_key_case()
<?php$age=array("Peter" =>"35","Ben" =>"37","J ...
- 微信小程序实现的校园查分助手
微信小程序实现的校园查分助手 背景 准备工作 思路 总结 背景 本科时我们的成绩系统距离比较远,网络延迟很严重,而且查分的时候需要跳转好多个页面,非常麻烦.出分的时候本来就够紧张的了..还得看页面转圈 ...
- 初次入坑解析的小程序(决定写代码风格的小程序)
初次入坑解析的小程序(决定写代码风格的小程序) 我是一个大二的学生,在接触小程序之前我是一个学计算机运维的,后来入了坑,在入坑以后开发了两个小程序. 第一个是为学校开发的一个考勤的小程序,样式是别人设 ...
最新文章
- 联想服务器升级微码文件,ThinkPad如何升级硬盘微码程序(适用于SL系列机器)
- PHP常用工具函数之手机号相关
- 洛谷 P1598 垂直柱状图【字符串】
- wsl ubuntu 版本_让Ubuntu滚动更新
- 如何修改Ubuntu Linux的时间
- 【ARM】ARM汇编程序设计(四) 选择结构
- 插入模板_WordPress在文章列表和内容页插入广告
- 《C和指针》——stdarg宏简介
- 印象笔记电脑版使用技巧_苏江:打造你的第二大脑,印象笔记的5个超级使用技巧...
- bzoj4974: [Lydsy1708月赛]字符串大师
- 序列化二叉树(C++)
- Sofia-SIP辅助文档六 - Sofia-SIP中的SIP和SDP特性
- 微信发布2018年各年龄段用户使用数据报告
- Glide用法总结--缓存与下载方法
- 塑胶模具设计:从材料优缺点来看塑料特性
- python opencv图片拼接、特征点匹配
- 手写由链表设计的简易队列
- 往年二本计算机分数线,全国一本、二本院校历年录取分数线汇总
- 1Mbps带宽到底能够达到什么效果,看看下面的介绍就懂了。
- 【签证】办理签证有特别要求的国家【签证办理必看】
热门文章
- 大数据安全和网络安全基础知识
- 加深 | Matlab图像实验操作基础(矩阵,九宫格、噪声处理)
- 高频直流电源在整改、降压和作用方面解决方案
- 北斗卫星广播星历计算卫星位置
- workstation服务启动报错。错误1075:依存服务不存在, 或已标记为删除的解决方法
- 关于双层原子台阶的形成机制
- 【Adobe安装】安装程序在Adobe Reader XI -Chinese Simplelified 安装完成之前被中断,错误代码150210
- ZTE10机顶盒中心服务器,中兴机顶盒现场配置工具ZTE_STB_Tools_V1.1.0_T07.01最新版
- FTP工具FileZilla Client出现中文乱码问题解决
- java kindeditor ssh,typecho KindEditor插件